Hiberate mapping type and saved Type
-
01-10-2019 - |
Question
I have Class CustomDate and that is referred in other class called Test.
public class CustomDate{
public String toString(){
return "20100829"
}
}
public class Test{
CustomDate date;
}
In Mapping file of Test
<property name="date" COLUMN="DATE">
I want to save only long value and type should be long type and i should not save date object and string value.long value will be this like:"20100829" actual date:29-08-2010. Because in feature if want compare dates in sql it may create problem, if it is long value, sql compare queries will fine. Do we have any way do it in Hibernate?
Solution
You'll have to implement the UserType
interface, and define the behavior of your new type.
<property name="twoStrings" type="com.company.CustomDate" />
Resources :
- Hibernate's wiki - UserType for persisting a Typesafe Enumeration with a VARCHAR column
- User Types in Hibernate - Integer Date Formats
- Hibernate documentation - Custom value types
Related posts :
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow