Hibernate

Learn about Hibernate, an object-relational mapping framework in Java.

What is Hibernate?

Hibernate is an open-source Java ORM persistence framework.

Hibernate can be used to simplify the mapping of SQL databases to Java Objects and includes its own query language (HQL), a Java API for writing queries, and the ability to specify mappings via annotations or XML.

Starting out

To begin using Hibernate:

  • Include the latest Hibernate jar using your preferred build framework.
  • Add your entity classes and mappings.
  • Define the Hibernate configuration.
  • Create the HQL queries necessary to implement your application.

For example, using Gradle, add the following to your dependencies:

compile 'org.hibernate:hibernate-core:5.2.12.Final'
compile 'org.hibernate:hibernate-hikaricp:5.2.12.Final'

Mappings

Each entity class should be public and have a public no-args constructor. Entities can be configured via an XML file in the same package and ending in .hbm.XML or via annotations within the entity. For simplicity, we will only cover the annotations.

Table annotation

...