了解hibernate @Type注释 [英] Understanding hibernate @Type annotation

查看:1083
本文介绍了了解hibernate @Type注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

官方hibernate文档
$ b

From the official hibernate documentation:


@ org.hibernate.annotations.Type覆盖默认的hibernate类型
used:这通常是不必要的,因为类型是正确的
由Hibernate推断

@org.hibernate.annotations.Type overrides the default hibernate type used: this is generally not necessary since the type is correctly inferred by 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;
}

我不明白。我们声明 @Type(type =org.hibernate.test.annotations.entity.MonetaryAmountUserType),但该方法的返回值的类型为 MonetaryAmount

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.

我预计在类型注释中声明的类型和返回值的类型应该是相同的类型。

I expected that the type declared within the type annotation and the type of the returned value should be the same type.

无法解释在 @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

There is difference between return type and @Type.

@Type 注释用于hibernate,即告诉您要在数据库中存储哪种数据。

@Type annotation is for hibernate i.e. to tell what type of data do you want to store in database.

让我们举一个简单的例子:

Let's take an simple example :

@Type(type="yes_no")
private boolean isActive; 

这里的返回类型是 boolean 存储在数据库中的数据将位于 Y N 格式中,而不是 true / false

Here return type is boolean but the value which get stored in database will be in Y or N format instead of true/false.

以同样的方式,您可以将对象映射到数据库列。查看此处获取更多详细信息。

In the same fashion you can map your object to database column. Check here for more detail explanation.

这篇关于了解hibernate @Type注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆