Hibernate.orgCommunity Documentation

Chapter 3. Tutorial Using Native Hibernate APIs and Annotation Mappings

3.1. The Hibernate configuration file
3.2. The annotated entity Java class
3.3. Example code
3.4. Take it further!

This tutorial is located within the download bundle under basic and illustrates

The contents are exactly the same as in Section 2.1, “The Hibernate configuration file”. The single difference is the mapping element at the very end naming the annotated entity class using the class attribute.

The entity class in this tutorial is org.hibernate.tutorial.annotations.Event which is still following JavaBean conventions. In fact the class itself is exactly the same as we saw in Section 2.2, “The entity Java class”, the only difference being the use of annotations to provide the metadata instead of a separate hbm.xml file.

The @javax.persistence.Entity annotation is used to mark a class as an entity. It's function is essentially the same as the class mapping element discussed in Section 2.3, “The mapping file”. Additionally the @javax.persistence.Table annotation is used to explicitly specify the table name (the default table name would have been EVENT).

@javax.persistence.Id marks the property defining the entity's identifier. @javax.persistence.GeneratedValue and @org.hibernate.annotations.GenericGenerator work in tandem to indicate that Hibernate should use Hibernate's increment generation strategy for this entity's identifier values.

Just as discussed in Section 2.3, “The mapping file”, the date property needs special handling to account for its special naming and its SQL type.

org.hibernate.tutorial.annotations.AnnotationsIllustrationTest is essentially the same as org.hibernate.tutorial.hbm.NativeApiIllustrationTest discussed in Section 2.4, “Example code”.

Try the following exercises: