Hibernate.orgCommunity Documentation
This tutorial is located within the download bundle under annotations
.
Objectives
Use annotations to provide mapping information
Use the native Hibernate APIs
The contents are identical to Section 2.1, “The Hibernate configuration file”, with one important
difference. 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
follows JavaBean conventions. In fact the class itself is identical to the one in Section 2.2, “The entity Java class”, except that annotations are used to provide the metadata,
rather than a separate hbm.xml
file.
The @javax.persistence.Entity
annotation is used to mark a class as an entity.
It functions the same as the class
mapping element discussed in Section 2.3, “The mapping file”. Additionally the
@javax.persistence.Table
annotation explicitly specifies the table
name. Without this specification, the default table name would be EVENT).
@javax.persistence.Id
marks the property which defines 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.
Example 3.3. Identifying basic properties
public String getTitle() { return title; } @Temporal(TemporalType.TIMESTAMP) @Column(name = "EVENT_DATE") public Date getDate() { return date; }
As 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”.