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

getReference vs find: JPA EntityManager Methods Compared

EntityManager find() loads an entity with a SELECT or returns null, while getReference() returns a proxy without SQL. See when each runs SQL, when EntityNotFoundException and LazyInitializationException are thrown, and how to set a foreign key without loading the entity.

Hibernate @NaturalId: Natural Ids, Lookups and Caching

Map a natural id such as a license plate with Hibernate’s @NaturalId, load entities with em.find(…, KeyType.NATURAL), handle mutable and composite natural ids, and cache lookups with @NaturalIdCache, with the SQL and statement counts from Hibernate 7.4.

No row with the given identifier exists: Hibernate 7 Fix

Hibernate throws “No row with the given identifier exists” when a proxy or an eager load asks for an id that has no row. See the real Hibernate 7.4 exceptions for getReference(), lazy and eager associations and concurrent deletes, and fix them with find(), orphan cleanup, a foreign key constraint or @NotFound.

Hibernate Ehcache Configuration: ehcache.xml for Hibernate 7

Configure Ehcache 3 for Hibernate 7 with ehcache.xml: one cache per region, templates, ttl/tti expiry, heap and offheap sizing, missing_cache_strategy, concurrency strategies and per-region statistics, all verified by a runnable project.

Errors in Named Queries: Fix the Hibernate Startup Error

Hibernate stops at startup with “Errors in named queries” when a JPQL named query uses a table name, a column name, SQL syntax or a broken parameter. See how to read NamedQueryValidationException in Hibernate 7 and fix each cause.

Hibernate Second Level Cache: How It Works (With Ehcache)

The Hibernate second level cache shares entity data across all sessions of an EntityManagerFactory. Learn the lookup order, what the cache stores, the concurrency strategies, collection and query caching, statistics and eviction with Hibernate 7.4 and Ehcache 3.

Hibernate First Level Cache: Persistence Context Explained

The Hibernate first level cache is the persistence context of one EntityManager. See with counted SQL on Hibernate 7.4 when find() and queries hit the database, how detach(), evict(), clear() and refresh() work, and how to keep long transactions small.

Hibernate Insert: persist(), merge() and HQL Insert Examples

To insert an object with Hibernate, we pass a new entity to persist() inside a transaction. Learn when the INSERT runs for IDENTITY, SEQUENCE and assigned ids, how merge() and HQL insert statements work, and what replaced Session.save() in Hibernate 7.

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.