Hibernate

Hibernate ORM enables developers to more easily write applications whose data outlives the application process. As an Object/Relational Mapping (ORM) framework, Hibernate is concerned with data persistence as it applies to relational databases (via JDBC).

Hibernate solves object-relational impedance mismatch problems by replacing direct persistence-related database accesses with high-level object handling functions. Hibernate’s primary feature is mapping from Java classes to database tables (and from Java data types to SQL data types). Hibernate also provides data query and retrieval facilities. It generates SQL calls and relieves the developer from manual result set handling and object conversion. Applications using Hibernate are portable to supported SQL databases with little performance overhead.

In addition to its own “native” API, Hibernate is also an implementation of the Java Persistence API (JPA) specification. As such, it can be easily used in any environment supporting JPA including Java SE applications, Java EE application servers, Enterprise OSGi containers, etc.

Hibernate supports lazy initialization, numerous fetching strategies and optimistic locking with automatic versioning and time stamping. Hibernate requires no special database tables or fields and generates much of the SQL at system initialization time instead of at runtime. Hibernate consistently offers superior performance over straight JDBC code, both in terms of developer productivity and runtime performance.

Hibernate was designed to work in an application server cluster and deliver a highly scalable architecture. Hibernate scales well in any environment: Use it to drive your in-house Intranet that serves hundreds of users or for mission-critical applications that serve hundreds of thousands. Hibernate is well known for its excellent stability and quality, proven by the acceptance and use by tens of thousands of Java developers.

Mapping Java classes to database tables is accomplished through the configuration of an XML file or by using Java Annotations.

Read below java tutorials for understanding various concepts involved into hibernate.

Related Tags

Tutorials

logo

How to Remove Orphan Entities in Hibernate?

In Hibernate, an orphan entity refers to a child entity that has been disassociated from its parent. If these orphan entities are not explicitly removed, they remain in the database and result in unwanted data. The orphanRemoval = true attribute on the mapping annotation on parent entity automatically removes the child entities from the database …

How to Save Child Entities using Hibernate?

One of the most essential concepts in Hibernate is working with parent-child entity relationships, where a parent entity contains references to one or more child entities. To ensure that we are saving/updating the child entities (along with the parent entity), we depend on the correct usage of annotations like @OneToOne, …

Hibernate Search Example with Lucene and Spring Boot

The Hibernate Search module works as a bridge between Hibernate ORM and full-text search engines such as Lucene or Elasticsearch. While performing a search, it enables us to work with JPA entities, and in the background, it transparently works with Lucene to provide a consistent experience. This Hibernate search guide …

Hibernate Soft Delete: @SoftDelete Example

Since version 6.4, Hibernate @SoftDelete annotation provides first-class support for soft delete feature and transparently manages the indicator column name and values.

Hibernate Tutorials

These tutorials have been published to help you start with persistence with Hibernate and JPA. These are updated to version Hibernate 6.x.

Mocking an In-memory JNDI DataSource

Learn to create and inject an in-memory DataSource that can be used to mock J2EE container provided JNDI DataSource without running a server.

Batch Processing with Hibernate/JPA

Learn to execute bulk SQL INSERT and UPDATE statements using Hibernate / JPA batch processing, and to configure session-specific batch sizes.

Stored Procedures with Hibernate

Hibernate provides support for executing the stored procedures and capturing their outputs using StoredProcedureQuery and ProcedureCall APIs.

Guide to Hibernate Search

Learn to configure full text and query index-based searches in Hibernate using backends like Lucene, Elasticsearch or OpenSearch.

Guide to Hibernate Interceptors

Learn to use Hibernate Interceptor for getting callbacks for persistence events and register it with Session and SessionFactory interfaces.

Guide to Pagination with Hibernate

Learn to implement and test the pagination functionality in hibernate using the HQL setFirstResult(), setMaxResults() and ScrollableResults.

Guide to Sorting using Hibernate

Learn to sort the entities fetched from the database using hibernate HQL, native SQL, Criteria queries and Comparator interface.

Deleting Entities with Hibernate

Learn to delete a single entity or a list of entities matching some criteria using hibernate native APIs and Jakarta persistence APIs.

[Solved] org.hibernate.QueryException: Cannot mix named and positional parameters

1. Reason we might face this exception if we are trying to execute stored procedure using JPA 2.1 and hibernate’s entity manager, and we are not using the entity manager’s registerStoredProcedureParameter() correctly. Incorrect usage may look like below OR the one you have written. The exception will look like this: …

Hibernate @NamedStoredProcedureQuery

Learn to execute SQL stored procedures with the help of @NamedStoredProcedureQuery annotation from Jakarta persistence API that helps in specifying the name of a stored procedure, its parameters, and its result type. 1. Project Setup The support for executing stored procedures using @NamedStoredProcedureQuery has been added in JPA 2.1. So …

Guide to Hibernate Criteria Queries

Learn about hibernate Criteria query interface, its basics, syntax, fetching data with multiple conditions including pagination and sorting.

Hibernate @Immutable Entities

Learn to use the Hibernate @Immutable annotation to create immutable entities, similar to immutable classes in Java, a rather well-known concept.

Guide to Lazy Loading in Hibernate

Hibernate fetches data from the database either in eager or lazy mode. Lazy loading refers to a strategy when data is loaded on demand.

Cascade Types in JPA and Hibernate

Learn JPA Cascade Types and how they are related to Hibernate Cascade Types. Learn how to control the orphan removal with an example.

Hibernate save(), update() and saveOrUpdate()

Learn the different methods for persisting and updating the entity states in the database using Hibernate Session APIs to use save(), update() and saveOrUpdate() methods under different usecases. Starting Hibernate 6.0, all save(), update() and saveOrUpdate() methods have been marked deprecated in favor of Jakarta persistence API provided persist() and …

Defining Hibernate Association Mappings

When we annotate the Java POJOs with JPA annotations and make them persistent entities, we can face situations where two entities can be related, and we must reference these entities from each other. This reference between the entities can be either uni-direction or in bi-direction. Creating these references is called …

Hibernate Entity LifeCycle

A hibernate entity, in context to persistence context or Session, can have 4 lifecycle states: transient, persistent, detached and removed.

[SOLVED] java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider

If you are trying to setup your project/ or adding dependency for hibernate 4 then you might face this error in your server logs. Stack-trace will be looking like this: Solution: Reason is that you have incompatible jar’s versions in your application classpath. To resolve this issue, make sure you …

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.