Understanding hibernate @Type annotation

From the official documentation of sleep mode :

@ org.hibernate.annotations.Type overrides the default hibernation type used: this is usually not required since the type is correctly inferred by Hibernate

The documentation has an example:

@Type(type="org.hibernate.test.annotations.entity.MonetaryAmountUserType") @Columns(columns = { @Column(name="r_amount"), @Column(name="r_currency") }) public MonetaryAmount getAmount() { return amount; } 

I do not understand this. We declare @Type(type="org.hibernate.test.annotations.entity.MonetaryAmountUserType") , but the return value of the method is of type MonetaryAmount .

I expected that the type declared in the type annotation and the return type should be of the same type.

Could someone @Type explain the actual type targets declared in the @Type annotation. Why is it different from the return type?

+12
source share
1 answer

There is a difference between the return type and the @Type type.

@Type annotation is for hibernation, i.e. @Type To indicate which data type you want to keep in the database.

Let's take a simple example:

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

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

In the same way, you can display your object in a database column. Check here for a more detailed explanation.

+19
source

Source: https://habr.com/ru/post/983956/


All Articles