Java Reference
In-Depth Information
Persisting Objects Using the Hibernate API with JPA Annotations
JPA annotations are standardized in the JSR-220 specification, so they're supported by all JPA-compliant
ORM frameworks, including Hibernate. Moreover, the use of annotations will be more convenient for
you to edit mapping metadata in the same source file.
The following Course class illustrates the use of JPA annotations to define mapping metadata:
package com.apress.springenterpriserecipes.course;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Table(name = "COURSE")
public class Course {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Long id;
@Column(name = "TITLE", length = 100, nullable = false)
private String title;
@Column(name = "BEGIN_DATE")
private Date beginDate;
@Column(name = "END_DATE")
private Date endDate;
@Column(name = "FEE")
private int fee;
// Constructors, Getters and Setters
Each entity class must be annotated with the @Entity annotation. You can assign a table name for
an entity class in this annotation. For each property, you can specify a column name and column details
using the @Column annotation. Each entity class must have an identifier defined by the @Id annotation.
You can choose a strategy for identifier generation using the @GeneratedValue annotation. Here, the
identifier will be generated by a table identity column.
Hibernate supports both native XML mapping files and JPA annotations as ways of defining
mapping metadata. For JPA annotations, you have to specify the fully qualified names of the entity
classes in hibernate.cfg.xml for Hibernate to read the annotations.
Search WWH ::

Custom Search