1

On the web, I found the following piece of code written within a Spring Data Configuration class that connects to a data source:

    @Bean
    @Primary
    public LocalContainerEntityManagerFactoryBean userEntityManager() {
        LocalContainerEntityManagerFactoryBean em
          = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(userDataSource());
        em.setPackagesToScan(
          new String[] { "some.package" });

        HibernateJpaVendorAdapter vendorAdapter
          = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        HashMap<String, Object> properties = new HashMap<>();
        properties.put("hibernate.hbm2ddl.auto",
          env.getProperty("hibernate.hbm2ddl.auto"));
        properties.put("hibernate.dialect",
          env.getProperty("hibernate.dialect"));
        em.setJpaPropertyMap(properties);

        return em;
    }

What I noticed is that within this Spring configuration class there is no bean method that provides a hibernate SessionFactory, and instead Hibernate properties are passed as map as in the code above. Can SessionFactory bean method be omitted if I specify hibernate properties as in the code above? How will Sessions get created in that case? Which way should be preferred?

Thanks in advance!

Dulat A
  • 21
  • 2
  • No. JPA uses an `entitymanagerFactory` and `Entitymanager` not `SessionFactory` and `Session`. – M. Deinum Dec 01 '21 at 08:52
  • Sorry, I think I didn't clarify. I want to configure a connection to a database and be able to use Hibernate. Do I need to configure a ```SessionFactory``` in that case? My question was more about the difference between declaring a separate ```SessionFactory``` bean method vs using ```setJpaPropertyMap``` in ```LocalContainerEntityManagerFactoryBean```. – Dulat A Dec 01 '21 at 09:21
  • Again no you don't. Hibernate is a JPA provider, so under the hood it will use Hibernate but you don't need to configure the hibernate specific classes. – M. Deinum Dec 01 '21 at 09:25
  • I assume you got the code from https://www.baeldung.com/spring-data-jpa-multiple-databases , are you following this example because you want to use mutliple databases or is that a coincidence? – PaulD Dec 01 '21 at 14:43
  • @PaulD yes, I am following their code. The thing is that I am adding a second database connection. In the first connection, properties such as hibernate dialect and validation were configured using SessionFactory bean method. But here it is done in a different way. So now I am a bit confused between the two, also I don't really understand what a SessionFactory is and whether it will be generated automatically if I just omit writing a separate bean method for it – Dulat A Dec 01 '21 at 16:48
  • @DulatA since the SessionFactory extends the EntityManagerFactory I doubt it makes a difference, you can also read up on a general take on SessionFactory vs EntityManagerFactory (https://stackoverflow.com/a/60354685/11764050) TLDR of that is to use EntityManager when possible and only use SessionFactory if you want Hibernate specific usecases. If this doesn't answer all of your points feel free to reply – PaulD Dec 02 '21 at 13:24

0 Answers0