Hibernate Type annotation examples 

Joined:
08/13/2009
Posts:
164

February 10, 2012 16:17:13    Last update: February 10, 2012 16:17:13
The annotation @org.hibernate.annotations.Type overrides the default hibernate mapping type used for a column. This can usually be omitted since Hibernate normaly infers the correct type to use.

But @Type is required in ambiguous scenarios such as a java.util.Date attribute, which can map to SQL DATE, TIME or TIMESTAMP. You use the @Type("timestamp") annotation to tell Hibernate that a timestamp converter should be used, which identifies an instance of org.hibernate.type.TimestampType.

@Type can also be used to identify custom type converters, which can be defined with @TypeDef at the class level:
@TypeDefs(
    {
    @TypeDef(
        name="dateToTimestamp",
        typeClass = DateToTimeStampType.class,
    )
    }
)

or with an xml file:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.big.converters">
	
	<!-- Type Definitions -->
	<typedef name="dateToTimestamp" class="DateToTimeStampType" />
</hibernate-mapping>
Share |
| Comment  | Tags