JPA Annotations - Hibernate Annotations - JournalDev
JPA Annotations - Hibernate Annotations - JournalDev
Hibernate
is the most popular implement of JPA specification and provides some additional annotations.
Today we will look into JPA annotations as well as Hibernate annotations with brief code snippets.
Java annotation is a form of metadata that can be added to Java source code. Java annotations
can be read from source files. It can also be embedded in and read from class files generated by
the compiler. This allows annotations to be retained by JVM at run-time.
JPA annotations are not part of standard JDK, so you will get it when you add any implementation
framework. For example, below hibernate maven dependency will get you JPA annotations too.
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
Let us look at some of the important JPA annotations. Note that these annotations are present
in javax.persistence package.
javax.persistence.Entity : Specifies that the class is an entity. This annotation can be applied
on Class, Interface of Enums.
import javax.persistence.Entity;
@Entity
@Table : It specifies the table in the database with which this entity is mapped. In the
example below the data will be stores in the “employee” table. Name attribute of @Table
annotation is used to specify the table name.
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "employee")
@Column : Specify the column mapping using @Column annotation. Name attribute of this
annotation is used for specifying the table’s column name.
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "employee")
@Column(name = "employee_name")
import javax.persistence.*;
@Entity
@Table(name = "employee")
@Id
@Column(name = "id")
@GeneratedValue : This annotation specifies the generation strategies for the values of
primary keys.
import javax.persistence.*;
@Entity
@Table(name = "employee")
@Id
@Column(name = "id")
@GeneratedValue(strategy=SEQUENCE, generator="ID_SEQ")
import javax.persistence.*;
@Entity
@Table(name = "employee")
@Version
@Column(name = "version")
@OrderBy : Sort your data using @OrderBy annotation. In example below, it will sort all
employees_address by their id in ascending order.
@OrderBy("id asc")
@Transient
@Lob
return employeeAddress;
The above set of annotation are most commonly used JPA annotations to define an entity.
We have another set of annotations that are used to specify the association mapping between
different tables and entities.
Tables ’employee’ and ’employeeDetail’ have one-to-one association and they share the same
primary key.
Tables ‘communication’ and ‘communicationDetail’ are linked by a foreign key. It is also a one to
one association.
Tables ‘communication’ and ’employee’ are linked using a foreign key in many-to-one
association with communication being the owner.
Tables ’employee’ and ’employeeStatus’ are linked through a foreign key in many-to-one
association with employee being the owner.
@OneToOne
Employee and EmployeeDetail entities share the same primary key and we can associate them
using @OneToOne and @PrimaryKeyJoinColumn.
In this case the id property of EmployeeDetail is not annotated with @GeneratedValue. The id
value of Employee will be used for used for id of EmployeeDetail.
@Entity
@Table(name = "employee")
@Id
@Column(name = "id")
@GeneratedValue
@OneToOne(cascade = CascadeType.MERGE)
@PrimaryKeyJoinColumn
@Entity
@Table(name = "employeeDetail")
@Id
@Column(name = "id")
Points to note:
@PrimaryKeyJoinColumn should be used for associated entities sharing the same primary key.
@JoinColumn & @OneToOne should be mappedBy attribute when foreign key is held by one
of the entities.
Communication and CommunicationDetail are linked through a foreign key, so @OneToOne and
@JoinColumn annotations can be used. In snippet mentioned below, the id genereated for
Communication will be mapped to ‘communication_id’ column of CommunicationDetail table.
@MapsId is used for the same.
@Entity
@Table(name = "communicationDetail")
@Id
@Column(name = "id")
@GeneratedValue
@OneToOne
@MapsId
@JoinColumn(name = "communicationId")
@Entity
@Table(name = "communication")
@Id
@Column(name = "ID")
@GeneratedValue
@ManyToOne
Many employees can share the same status. So, employee to employeeStatus is a many to one
relation. @ManyToOne annotation can be used for the same.
@Entity
@Table(name = "employee")
@ManyToOne
@JoinColumn(name = "statusId")
@OneToMany
Employee to Communication will be a one-to-many relationship. The owner of this relationship is
Communication so, we will use ‘mappedBy’ attribute in Employee to make it bi-directional
relationship.
@Entity
@Table(name = "employee")
@OrderBy("firstName asc")
@PrimaryKeyJoinColumn
This annotation is used to associate entities sharing the same primary key.
@Entity
@Table(name = "employee")
@Id
@Column(name = "id")
@GeneratedValue
@OneToOne(cascade = CascadeType.MERGE)
@PrimaryKeyJoinColumn
@JoinColumn
@JoinColumn annotation is used for one-to-one or many-to-one associations when foreign key is
held by one of the entities.
@ManyToOne
@JoinColumn(name = "statusId")
@JoinTable : @JoinTable and mappedBy should be used for entities linked through an
association table.
@MapsId : Two entities with shared key can be persisted using @MapsId annotation.
@OneToOne
@MapsId
@JoinColumn(name = "communicationId")
Table per class hierarchy – single table per Class Hierarchy Strategy.
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="cartype",
discriminatorType=DiscriminatorType.STRING )
@DiscriminatorValue("Car")
@Entity
@DiscriminatorValue("BMW")
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@Entity
@PrimaryKeyJoinColumn
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@DiscriminatorColumn: As the name suggests this column is the descriminator and this
annotation specifies the discriminator column for the SINGLE_TABLE and JOINED Inheritance
mapping strategies.
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="cartype",
discriminatorType=DiscriminatorType.STRING )