简体   繁体   中英

Understanding hibernate @Type annotation

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

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 .

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?

There is difference between return type and @Type .

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

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM