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
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 …
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, …
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 …
Since version 6.4, Hibernate @SoftDelete annotation provides first-class support for soft delete feature and transparently manages the indicator column name and values.
These tutorials have been published to help you start with persistence with Hibernate and JPA. These are updated to version Hibernate 6.x.
Learn to create and inject an in-memory DataSource that can be used to mock J2EE container provided JNDI DataSource without running a server.
Learn to execute bulk SQL INSERT and UPDATE statements using Hibernate / JPA batch processing, and to configure session-specific batch sizes.
Hibernate provides support for executing the stored procedures and capturing their outputs using StoredProcedureQuery and ProcedureCall APIs.
Learn to use the Hibernate aggregate functions used to query a calculated value for a field among all entities matching the given criteria.
Learn to configure full text and query index-based searches in Hibernate using backends like Lucene, Elasticsearch or OpenSearch.
Learn to use Hibernate Interceptor for getting callbacks for persistence events and register it with Session and SessionFactory interfaces.
Learn to implement and test the pagination functionality in hibernate using the HQL setFirstResult(), setMaxResults() and ScrollableResults.
Learn to sort the entities fetched from the database using hibernate HQL, native SQL, Criteria queries and Comparator interface.
Learn to delete a single entity or a list of entities matching some criteria using hibernate native APIs and Jakarta persistence APIs.
Learn to use the Hibernate’s native event callback mechanism and annotation based callback events from the new Jakarta persistence API.
Learn to initialize the EnitytManager using the XML configuration and the programmatic configuration in hibernate 6 and Jakarta persistence.
Learn to map Java 8 classes (LocalDate, LocalTime or LocalDateTime etc) to SQL types while storing the time-based values in the database.
Learn to setup and configure the L2 (second-level cache) in Hibernate 6 using Ehcache 3 using its JCache API and EhcacheCachingProvider.
Learn to solve the IllegalArgumentException: Unable to locate persister error while migrating from hibernate 5 to hibernate 6.
Learn to use Hibernate validator CDI module to inject default bean validation factory implementation i.e. ValidatorFactory and Validator.
Hibernate validator example to validate the Java beans using Jakarta bean validation API. Learn to configure messages and interpolation.
Learn to use hibernate initialize() method and enable_lazy_load_no_trans property to force initialization of proxy entity or collection.
If you are working on Hibernate and HSqlDB, and you are trying to fetch collection of entities, then you may face this error. For single entity you may or may not find this error. Exception will look like this: Solution This error is due to version mismatch of hibernate and …
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: …
We might face this exception if we are trying to execute any query OR stored procedure, and parameters are not set in the correct datatype.
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 …
JPA delete query example. Learn to use native SQL DELETE query in JPA using EntityManager’s executeUpdate() method.
You may get NotYetImplementedException exception when you are trying to include some native SQL queries in your project as named native queries, and you do not expect any result after execution of those sql queries. e.g. SQL UPDATE queries which does not return any result. Problem Let’s say you want …
JPA named query update example. Learn to use @NamedNativeQuery annotation to execute SQL update queries using EntityManager.cexecuteUpdate().
Learn to define and execute a named native SQL query using Hibernate @NamedNativeQuery annotation with examples.
Learn about hibernate Criteria query interface, its basics, syntax, fetching data with multiple conditions including pagination and sorting.
Learn what is hibernate query language, HQL syntaxes, named and native SQL queries, associations and aggregations with examples.
Learn to use the Hibernate @Immutable annotation to create immutable entities, similar to immutable classes in Java, a rather well-known concept.
Learn the basics and usage of JPA hibernate annotations used for creating entities, associations and named queries.
Learn the drawbacks and benefits of using the annotations configuration over XML files to better know when to apply which format.
Hibernate fetches data from the database either in eager or lazy mode. Lazy loading refers to a strategy when data is loaded on demand.
Learn JPA Cascade Types and how they are related to Hibernate Cascade Types. Learn how to control the orphan removal with an example.
Hibernate merge() does exactly the opposite of what refresh() does i.e. causing the updates to database with values from the detached entity.
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 …
Learn the difference between get() vs load() methods that are used to fetch entity by id from the database using Hibernate.
Learn why it is important to implement hashCode() and equals() methods in hibernate entity classes. How does it affect different Sessions.
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 …
A hibernate entity, in context to persistence context or Session, can have 4 lifecycle states: transient, persistent, detached and removed.
Learn to configure C3P0 connection pooling with Hibernate 6. Learn how to enforce c3p0 over other connection polls and debug connection leaks.
Learn to connect to an in-memory database (such as H2 or Hsqldb) from the JUnit 5 unit tests. This will help in writing tests that do not depend on a live database connection.
Learn to bootstrap SessionFactory with StandardServiceRegistryBuilder, StandardServiceRegistry and Metadata in Hibernate versions 5 and 6.
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: If you have anything like below declaraions in your hibernate configuration for “sessionFactory” then remove it. OR Once you …
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 …
1. Reason If we are trying to setup your project/ or adding a dependency for hibernate then we might face this error in your server logs. Stack-trace will be looking like this: 2. Solution The reason is that we have incompatible jar versions in our application classpath. To resolve this …