Hibernate association and join example

Learn how use the associations in Hibernate.

Hibernate association and join example

Example program of Associations and Joins in Hibernate framework

In this section we will learn about the Hibernate Associations and Joins, which is very important concept of Hibernate framework. Hibernate association allows the developer to represent the database relationships in object oriented way. In this you will learn about the database relationship and the Hibernate relationships. You will also learn how to present these relationships in Hibernate. Hibernate relationships eases the development of persistence layer of complex application involving various types of relationships such as one-to-one, one-to-many etc. We have also provides the appropriate examples of Hibernate associations and joins to help you understand the concepts in better way.

Database Relation ships

All the modern relational databases are very advance and supports various types of relationships for managing the complex relational data in much more intuitive way. Database relationships are importance concept and you should know about this. The relationship concept in database allows you to define relationship (or connection) between the tables of a database. Once the relationship is correctly defined among the tables of the database, you can perform cross-table queries. This way of performing cross-table queries is knows as joins. Database supports simple as well as complex cross-table queries (joins). Relational databases supports following types of relationships:

  • One-to-One
  • One-to-Many
  • Many-to-One
  • Many-to-Many

These four types of relationships are sufficient for managing the complex business data.

Hibernate Association

The Hibernate ORM supports the association relationships and following components takes parts:

  • Java Class(s) - The persistence classes or simply POJO is used in the Java program. Hibernate framework maps the object with the table of the relational database.
     
  • Database Table - The Database table is responsible to saving the data into table(s).
     
  • Annotations or mapping files (.hmb) - You can either use the Java annotations or the mapping xml files (.hmb) for defining the mapping information for your application. Hibernate ORM uses these meta-data for creating the relationships.

If you use the annotations for mapping the relationship then you can use any of the following annotations as per your requirement:

  • @OneToOne
  • @OneToMany
  • @ManyToOne
  • @ManyToMany

Types of Hibernate Association mappings

Hibernate framework supports following types of association mappings:

  • One-to-One
  • One-to-Many
  • Many-to-One
  • Many-to-Many

Further each of the above associations are of following types:

  • Unidirectional Associations - In this association one table is linked to second table but second table is not linked to first table.
  • Bidirectional Associations - In this association first table is linked to second table and second table is also linked to first table.

Here is the examples of Hibernate associations

Unidirectional Association

Unidirectional Association with join Table

  • One to Many XML Example
  • One to Many Annotation Example
  • Many to One XML Example
  • Many to One Annotation Example
  • One to One XML Example
  • One to One Annotation Example
  • Many to Many XML Example
  • Many to Many Annotation Example

Bidirectional Association

  • One to Many
  • One to One XML Example
  • One to One Annotation Example

Bidirectional Association with join Table

  • One to Many XML Example
  • Many to Many XML Example

Check more tutorials at Hibernate Tutorial home page.