Back to Hibernate Orm

Hibernate ORM Javadocs

shared/javadoc/overview.html

7.3.39.7 KB
Original Source

Hibernate ORM Javadocs

Hibernate is a library for object/relation mapping (ORM). It provides:

  • a native API centered around {@link org.hibernate.SessionFactory}, {@link org.hibernate.Session}, and {@link org.hibernate.StatelessSession},
  • an implementation of the Java (or Jakarta) Persistence API (JPA), where the equivalent central interfaces are {@link jakarta.persistence.EntityManagerFactory} and {@link jakarta.persistence.EntityManager},
  • a set of mapping annotations which augment the O/R mapping annotations defined by JPA, and which may be used with either API, and
  • compile-time tooling for writing more type-safe code.

Native API

Along with {@link org.hibernate.SessionFactory} and {@link org.hibernate.Session}, applications using the native API will often make use of the following interfaces:

  • {@link org.hibernate.jpa.HibernatePersistenceConfiguration} or {@link org.hibernate.cfg.Configuration} to configure and bootstrap Hibernate,
  • {@link org.hibernate.Cache} to manage the second-level cache,
  • {@link org.hibernate.Transaction} to control local transactions,
  • {@link org.hibernate.query.SelectionQuery} and {@link org.hibernate.query.MutationQuery} to execute HQL queries,
  • {@link org.hibernate.query.NativeQuery} to execute native SQL queries,
  • {@link org.hibernate.query.specification.SelectionSpecification} and {@link org.hibernate.query.specification.MutationSpecification} for working with dynamic {@linkplain org.hibernate.query.restriction.Restriction restrictions} and {@linkplain org.hibernate.query.Order ordering},
  • {@link org.hibernate.Filter} to manage filters,
  • {@link org.hibernate.query.criteria.HibernateCriteriaBuilder} to construct criteria queries, and
  • {@link org.hibernate.relational.SchemaManager} to execute DDL in tests.

JPA

The JPA interfaces are defined by the JPA specification. For details see the latest specification along with the API documentation for the package {@link jakarta.persistence}.

Along with {@link jakarta.persistence.EntityManagerFactory} and {@link jakarta.persistence.EntityManager}, programs based on the standard JPA API often use:

  • {@link jakarta.persistence.Persistence} and {@link jakarta.persistence.PersistenceConfiguration} to bootstrap Hibernate via JPA,
  • {@link jakarta.persistence.TypedQuery} to execute queries,
  • {@link jakarta.persistence.EntityGraph} to control the boundaries of fetched data,
  • {@link jakarta.persistence.EntityTransaction} to control local transactions,
  • {@link jakarta.persistence.Cache} to manage the second-level cache,
  • {@link jakarta.persistence.SchemaManager} to execute DDL in tests,
  • {@link jakarta.persistence.criteria.CriteriaBuilder} to construct criteria queries,
  • {@link jakarta.persistence.metamodel.Metamodel} to implement generic code which makes use of persistent entity classes in a reflective fashion, and
  • {@link jakarta.persistence.criteria.CriteriaBuilder} to build JPA criteria queries.

Note that since Hibernate 5.2, the native API extends the JPA API rather than wrapping it. For example, SessionFactory extends EntityManagerFactory, and Session extends EntityManager.

It's always possible to fall back from JPA interfaces to native APIs, by calling {@link jakarta.persistence.EntityManager#unwrap entityManager.unwrap(Session.class)}, {@link jakarta.persistence.EntityManagerFactory#unwrap entityManagerFactory.unwrap(SessionFactory.class)}, or {@link jakarta.persistence.Query#unwrap query.unwrap(Query.class)}. In certain cases it's also possible to access native functionality by passing a {@linkplain org.hibernate.jpa.SpecHints JPA-defined} or {@linkplain org.hibernate.jpa.HibernateHints Hibernate-defined} hint, at the cost of a loss of type-safety.

These packages define additional extensions to the JPA APIs:

  • {@link org.hibernate.query.criteria} packages extensions to {@link jakarta.persistence.criteria}, and
  • {@link org.hibernate.metamodel.model.domain} packages extensions to {@link jakarta.persistence.metamodel}.

Mapping annotations

The mapping annotations defined by the JPA specification provide a foundation for expressing object/relational mappings in Hibernate and other JPA implementations.

The annotations in the package {@link org.hibernate.annotations} extend this foundation and accommodate more specialized requirements. These annotation are not tied to the native API, and may be used in conjunction with the JPA API.

The full power of Hibernate can only be unlocked via judicious use of these extra annotations.

XML-based mappings

Annotation-based mappings are the best choice for most users, but Hibernate offers XML-based mappings as an alternative.

  • The JPA-standard XML schema is orm_3_0.xsd.
  • Hibernate extends this schema with some additional mapping elements. The extended schema is mapping-3.1.0.xsd.
  • Prior to the existence of JPA, Hibernate had its own format for XML-based mappings, which still works, though it has not been improved in a long time. The DTD is hibernate-mapping-3.0.dtd

Bootstrapping Hibernate

There are four basic ways to obtain an instance of Hibernate:

  • as a JPA persistence provider, by using {@link jakarta.persistence.Persistence#createEntityManagerFactory},
  • by using the "simplified" {@link org.hibernate.cfg.Configuration} API,
  • for those who enjoy dirty hands, by using the APIs in {@link org.hibernate.boot}, or
  • in a container environment like WildFly or Quarkus, by letting the container take care of the bootstrap process and of injecting the {@code EntityManagerFactory} or {@code SessionFactory}.

All major Java application servers and microservice frameworks come with built-in support for Hibernate. Such container environments also typically feature facilities to automatically manage the lifecycle of a {@code EntityManager} or {@code Session} and its association with container-managed transactions.

Example configuration files for JPA and native usage may be found {@linkplain org.hibernate.cfg here}. A comprehensive list of configuration properties understood by Hibernate may be found in the class {@link org.hibernate.cfg.AvailableSettings}. Most sensible programs will only ever need to use a tiny handful of them.

Annotations driving compile-time tooling

The annotations defined by {@link org.hibernate.annotations.processing} instruct the Metamodel Generator to {@linkplain org.hibernate.annotations.processing.CheckHQL validate HQL at compile time}, and to automatically generate the implementation of {@linkplain org.hibernate.annotations.processing.Find finder methods} and {@linkplain org.hibernate.annotations.processing.HQL query methods}.

Hibernate offers an enormous wealth of extension points for customizing almost any aspect of its implementation. Most of these extension points are far too technical to be of interest to the typical application developer.

However, the following extension points are of quite general interest:

  • {@link org.hibernate.boot.model.naming} allows the quantity of repetitive O/R mapping metadata to be minimized via the use of naming strategies,
  • {@link org.hibernate.type.descriptor.jdbc} and {@link org.hibernate.type.descriptor.java} contain the built-in {@code JdbcType}s and {@code JavaType}s for "compositional" basic attribute type mappings,
  • {@link org.hibernate.usertype} defines support for user-defined custom attribute types,
  • {@link org.hibernate.generator} defines support for generated attribute values,
  • {@link org.hibernate.context.spi} defines support for context-bound "current" sessions and contextual multi-tenancy, and
  • {@link org.hibernate.binder} allows for user-defined mapping annotations.

More advanced extension points include:

  • {@link org.hibernate.dialect} provides a framework for modelling dialects of SQL,
  • {@link org.hibernate.cache.spi} defines an SPI for integrating with second-level cache providers,
  • {@link org.hibernate.engine.jdbc.connections.spi} defines an SPI for integrating with JDBC connection pools.

Finally, Hibernate ORM Core is itself a framework for advanced extensions like Hibernate Search, Hibernate Reactive, and Envers, which do much more than just implementing a single well-defined extension point. The starting points for such extensions are found in the packages {@link org.hibernate.integrator.spi} and {@link org.hibernate.event.spi}.

Package categories

The organization of code into packages is based on the following classification:

  • API packages include classes and interfaces which are used directly by a typical application. These packages never have spi nor internal in their name, and are not under the namespace org.hibernate.testing.
  • SPI packages include classes and interfaces which are used by integrators, library developers, and framework developers to develop extensions to Hibernate, or to alter its behavior in some way. These packages usually have spi in their name.
  • Some classes and interfaces are considered part of the internal implementation of Hibernate. This category includes packages with internal in their name, along with any class or interface annotated {@link org.hibernate.Internal @Internal}. Clients should avoid depending directly on these types.
  • The hibernate-testing module, and the namespace org.hibernate.testing contain testing support used in the Hibernate test suite.

More information

Complete documentation may be found online at http://hibernate.org/orm/documentation/.