Hibernate.orgCommunity Documentation
This tutorial is located within the download bundle under basic
and illustrates
using annotations to provide mapping information
using the native Hibernate APIs
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.
Example 3.1. Identifying the class as an entity
@Entity
@Table( name = "EVENTS" )
public class Event {
...
}
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).
Example 3.2. Identifying the identifier property
@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy = "increment")
public Long getId() {
return id;
}
@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.
Example 3.3. Identifying basic properties
public String getTitle() {
return title;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "EVENT_DATE")
public Date getDate() {
return date;
}
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:
With help of the Developer Guide, add an association to
the Event
entity to model a message thread.
With help of the Developer Guide, add a callback to
receive notifications when an Event
is created, updated or deleted. Try
the same with an event listener.
Copyright © 2004 Red Hat, Inc.