Java 理解 hibernate @Type 注解
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29118210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Understanding hibernate @Type annotation
提问by user3663882
From the official hibernate documentation:
从官方休眠文档:
@org.hibernate.annotations.Type overrides the default hibernate type used: this is generally not necessary since the type is correctly inferred by Hibernate
@org.hibernate.annotations.Type 覆盖使用的默认休眠类型:这通常不是必需的,因为该类型已由 Hibernate 正确推断
There is an example from the documentation:
文档中有一个例子:
@Type(type="org.hibernate.test.annotations.entity.MonetaryAmountUserType")
@Columns(columns = {
@Column(name="r_amount"),
@Column(name="r_currency")
})
public MonetaryAmount getAmount() {
return amount;
}
I don't understand that. We declare @Type(type="org.hibernate.test.annotations.entity.MonetaryAmountUserType")
but the method's return value has the type MonetaryAmount
.
我不明白。我们声明@Type(type="org.hibernate.test.annotations.entity.MonetaryAmountUserType")
但方法的返回值具有类型MonetaryAmount
。
I expected that the type declared within the type annotation and the type of the returned value should be the same type.
我希望类型注释中声明的类型和返回值的类型应该是相同的类型。
Couldn't someone explain the actual purposes of the type, declared within the @Type
annotation. Why is it differ from the returned type?
有人无法解释在@Type
注释中声明的类型的实际用途。为什么它与返回的类型不同?
采纳答案by Waheed
There is difference between return type and @Type
.
返回类型和@Type
.
@Type
annotation is for hibernate i.e. to tell what type of data do you want to store in database.
@Type
注释用于休眠,即告诉您要在数据库中存储什么类型的数据。
Let's take a simple example:
我们举一个简单的例子:
@Type(type="yes_no")
private boolean isActive;
Here return type is boolean
but the value which gets stored in the database will be in Y
or N
format instead of true
/false
.
这里的返回类型是boolean
但存储在数据库中的值将采用Y
orN
格式而不是true
/ false
。
In the same fashion you can map your object to a database column. Check herefor more detailed explanation.
以同样的方式,您可以将对象映射到数据库列。请点击这里获取更详细的解释。