Confusion about EntityManagerFactory and SessionFactory with Hibernate 5.3, Spring Data JPA 2.1.4 and Spring 5.1

3k Views Asked by At

I tried to figure out the new mechanism for integrating Hibernate and Spring Data JPA. I followed the example provided on https://www.baeldung.com/hibernate-5-spring but to no avail. Further research brought me an issue on Github by Juergen Hoeller saying:

[...] This covers a lot of ground for a start: With Hibernate 5.2 and 5.3, LocalSessionFactoryBean and HibernateTransactionManager serve as a 99%-compatible replacement for LocalContainerEntityManagerFactoryBean and JpaTransactionManager in many scenarios, allowing for interaction with SessionFactory.getCurrentSession() (and also HibernateTemplate) next to @PersistenceContext EntityManager interaction within the same local transaction (#21454). That aside, such a setup also provides stronger Hibernate integration (#21494, #20852) and more configuration flexibility, not being constrained by JPA bootstrap contracts.

And corresponding Javadoc for the LocalSessionFactoryBean class stating:

Compatible with Hibernate 5.0/5.1 as well as 5.2/5.3, as of Spring 5.1. Set up with Hibernate 5.3, LocalSessionFactoryBean is an immediate alternative to LocalContainerEntityManagerFactoryBean for common JPA purposes: In particular with Hibernate 5.3, the Hibernate SessionFactory will natively expose the JPA EntityManagerFactory interface as well, and Hibernate BeanContainer integration will be registered out of the box. In combination with HibernateTransactionManager, this naturally allows for mixing JPA access code with native Hibernate access code within the same transaction.

I implemented a simple example project with Spring Boot 2.1.2.RELEASE. It provides a simple configuration (the same as in the Baeldung example above) and connects to a PostgreSQL database. Furthermore, it uses a model and a repository for, theoretically, working with the data. The classes look like this:

DemoApplication.java

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication
{

    public static void main(String[] args)
    {
        SpringApplication.run(DemoApplication.class, args);
    }
}

BasicConfig.java

package com.example.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.repository.config.BootstrapMode;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import org.postgresql.Driver;
import java.util.Properties;

@Configuration
@EnableJpaRepositories
public class BasicConfig
{
    @Bean
    public LocalSessionFactoryBean sessionFactory()
    {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan("com.example.demo");
        sessionFactory.setHibernateProperties(hibernateProperties());

        return sessionFactory;
    }

    @Bean
    public DataSource dataSource()
    {
        SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
        dataSource.setDriverClass(Driver.class);
        dataSource.setUrl("jdbc:postgresql://localhost:5432/backend");
        dataSource.setUsername("backend");
        dataSource.setPassword("backend");

        return dataSource;
    }

    @Bean
    public PlatformTransactionManager hibernateTransactionManager()
    {
        HibernateTransactionManager transactionManager
                = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory().getObject());
        return transactionManager;
    }

    private final Properties hibernateProperties()
    {
        Properties hibernateProperties = new Properties();
        hibernateProperties.setProperty(
                "hibernate.hbm2ddl.auto", "create-drop");
        hibernateProperties.setProperty(
                "hibernate.dialect", "org.hibernate.dialect.PostgreSQL95Dialect");

        return hibernateProperties;
    }
}

Model.java

package com.example.demo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "model")
public class Model
{
    @Id
    @GeneratedValue
    @Column(name = "id", unique = true, nullable = false)
    private Long id;

    @Column(name = "name")
    private String name;
}

DemoRepository.java

package com.example.demo;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface DemoRepository extends JpaRepository<Model, Long>
{
}

As soon as I add the DemoRepository the App does not start anymore because:

A component required a bean named 'entityManagerFactory' that could not be found.


Action:

Consider defining a bean named 'entityManagerFactory' in your configuration.

Complete error message:

Exception encountered during context initialization - cancelling refresh
attempt: org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'demoRepository': 
Cannot create inner bean '(inner bean)#6c5ca0b6' of type  [org.springframework.orm.jpa.SharedEntityManagerCreator]  
while setting bean property 'entityManager';  
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name '(inner bean)#6c5ca0b6': 
Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No bean named 'entityManagerFactory' available

I was under the impression that the SessionFactorynow properly implements and exposes the EntityManagerFactory but that does not seem to be the case. I am pretty sure that there is a flaw in my implementation and that the example from Baeldung actually works properly. I hope someone can point it out to me and help me understand my mistake.
Thanks to everyone in advance.

Dependencies:

  • spring-data-jpa:2.1.4.RELEASE
  • spring-core:5.1.4.RELEASE
  • spring-orm:5.1.4.RELEASE
  • hibernate-core:5.3.7.Final
  • spring-boot:2.1.2.RELEASE

gradle.build

buildscript {
    ext {
        springBootVersion = '2.1.2.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.postgresql:postgresql'
}
1

There are 1 best solutions below

0
On BEST ANSWER

After some trial-and-error we figured out what was wrong with the configuration. The following (abridged) configuration works:

@Configuration
@EnableJpaRepositories(
        basePackages = "com.example.repository",
        bootstrapMode = BootstrapMode.DEFERRED
)
@EnableTransactionManagement
@Profile({ "local", "dev", "prod" })
public class DatabaseConfig
{
    @Bean
    public LocalSessionFactoryBean entityFactoryBean(
            final DataSource dataSource
    )
    {
        final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();

        sessionFactory.setDataSource(dataSource);
        sessionFactory.setPackagesToScan("com.example.model");

        return sessionFactory;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation()
    {
        return new PersistenceExceptionTranslationPostProcessor();
    }

    @Bean
    public HibernateTransactionManager transactionManager(
            final SessionFactory sessionFactory,
            final DataSource dataSource
    )
    {
        final HibernateTransactionManager transactionManager = new HibernateTransactionManager();

        transactionManager.setSessionFactory(sessionFactory);
        transactionManager.setDataSource(dataSource);

        return transactionManager;
    }
}

The LocalSessionFactoryBean can be named entityFactoryBean and Spring is still able to autowire a SessionFactoy for the hibernateTransactionManager. I hope this helps if anybody else has a similar problem.