Category Archives: Hibernate

Hibernate 7 Hello World: A Working Project from Empty Folder to First Insert

Most Hibernate Hello World tutorials get you to a running INSERT in about 10 minutes. They also leave out three things that will bite you the moment you move past the trivial case.

First: the import packages. Hibernate 7 requires jakarta.persistence.*, not javax.persistence.*. Every tutorial written before 2022 uses the old namespace. If you copy the entity annotation from a pre-Hibernate 6 guide, the annotations will not be recognised and you will spend 30 minutes wondering why your table never appears.

Second: the persistence.xml location trap in Maven multi-module setups. The file belongs at src/main/resources/META-INF/persistence.xml in the module that contains your entity classes. Putting it in the wrong module produces a silent failure at bootstrap.

Third: the LazyInitializationException you will hit in your second example. The first example always works because you access everything inside one open session. The second example accesses an association after the session closes. This post calls that out explicitly so you see it coming.

Everything else is the standard step-by-step: Maven setup, entity, configuration, utility class, first insert. The code is complete and runs without modification on Java 17+.

Three Things This Guide Gets Right That Most Hello World Tutorials Skip

Hibernate 7 is not just a minor update; it’s optimized for modern environments like Jakarta EE 11 and Java 17/21/25+. It leverages the latest features of the JPA (Jakarta Persistence API) to provide a more type-safe and performant way to interact with your data.

High-Level Architecture Overview

Understanding the flow of data is crucial for mastering any ORM. Here is the architectural stack for this tutorial:

Java ApplicationHibernate SessionJPA Entity ManagerDialectDatabase (H2/MySQL)

Continue reading Hibernate 7 Hello World: A Working Project from Empty Folder to First Insert

Solved: Bean property ‘configurationClass’ is not writable or has an invalid setter method

Every developer has those moments where a seemingly simple task turns into a head-scratching puzzle. This error, encountered while trying to configure a custom LocalSessionFactoryBean in Spring Boot, is a perfect example of a subtle type mismatch causing a cryptic failure.


The Scenario and the Error

The goal was to set up a custom SessionFactory bean using Spring’s LocalSessionFactoryBean. The initial, problematic configuration was:

@Configuration
public class HibernateConfig {
    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        // … set data source, hibernate properties …
        
        // This line caused the error!
        sessionFactory.setConfigurationClass(org.hibernate.cfg.Configuration.class); 
        
        return sessionFactory;
    }
}

This configuration resulted in the following exception upon application startup:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': 
Bean property 'configurationClass' is not writable or has an invalid setter method. 
Does the parameter type of the setter match the return type of the getter?
Continue reading Solved: Bean property ‘configurationClass’ is not writable or has an invalid setter method

Solved: java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider

Encountering a java.lang.NoClassDefFoundError can be one of the most frustrating issues when working with Java applications. This particular error — org/hibernate/cache/CacheProvider — is a common stumbling block for developers upgrading or mixing Hibernate versions. Let’s break down what it means and how to fix it.

Understanding the Error

The NoClassDefFoundError occurs when the JVM tries to load a class by its fully qualified name but cannot find its definition at runtime — even though the class existed at compile time. In this case, org.hibernate.cache.CacheProvider was the standard interface for Hibernate second-level cache integration in Hibernate 3.x. It was deprecated in Hibernate 4 and completely removed in Hibernate 5+, replaced by org.hibernate.cache.spi.RegionFactory.

Continue reading Solved: java.lang.NoClassDefFoundError: org/hibernate/cache/CacheProvider