JPA Annotations - Hibernate Annotations With Examples

JPA annotations are used in mapping java objects to the database tables, columns etc. 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.

JPA Annotations – Hibernate Annotations

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.

JPA Annotations for mapping java object to database table

Let us look at some of the important JPA annotations. Note that these annotations are present in javax.persistence package.

  1. javax.persistence.Entity: Specifies that the class is an entity. This annotation can be applied on Class, Interface of Enums.
  2. @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.
  3. @Column: Specify the column mapping using @Column annotation. Name attribute of this annotation is used for specifying the table’s column name.
  4. @Id: This annotation specifies the primary key of the entity.
  5. @GeneratedValue: This annotation specifies the generation strategies for the values of primary keys.
  6. @Version: We can control versioning or concurrency using this annotation.
  7. @OrderBy: Sort your data using @OrderBy annotation. In example below, it will sort all employees_address by their id in ascending order.
  8. @Transient: Every non static and non-transient property of an entity is considered persistent, unless you annotate it as @Transient.
  9. @Lob: Large objects are declared with @Lob.

The above set of annotation are most commonly used JPA annotations to define an entity.

Hibernate Annotations for Mapping between tables

We have another set of annotations that are used to specify the association mapping between different tables and entities.

We will take an example considering the below mentioned scenario.

  • 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.

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.

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.

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.

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.

This annotation is used to associate entities sharing the same primary key.

@JoinColumn annotation is used for one-to-one or many-to-one associations when foreign key is held by one of the entities.

@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.

Hibernate Annotations for inheritance mapping

Now let us try to understand the inheritance mapping annotation in Hibernate.

Hibernate supports the three basic inheritance mapping strategies:

  • table per class hierarchy
  • table per subclass
  • table per concrete class

we will consider example for each type.

  1. Table per class hierarchy – single table per Class Hierarchy Strategy.
  2. Table per class/subclass – joined subclass Strategy.
  3. Table per concrete class.
  4. @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.

That’s all for JPA and Hibernate annotations.

Reference: JSR 338, Hibernate API Docs

By admin

Leave a Reply

%d bloggers like this: