3

I'm migrating an application from Spring Boot 1.5.14.RELEASE to 2.0.4.RELEASE. I'm using Java 8 and Maven 3.5.4 if it matters. Please, see the complete project reproducing the issue here:

https://github.com/dmitrysenkovich/entity-manager-factory-test-example

I'm not sure if it works when running with spring-boot:run but that doesn't matter. So, please, run DaoTest and you will get:

BeanCurrentlyInCreationException: Error creating bean with name 'sessionFactory': Requested bean is currently in creation: Is there an unresolvable circular reference?

It is caused by entityManagerFactory being in creation at the moment it is needed in PersistenceTestConfig to create a sessingFactory bean. Seems like that it because Hibernate 5.2 SessionFactory extends EntityManagerFactory. But I'm stil not sure what the exact issue is.

Many thanks for any help!

Dmitry Senkovich
  • 5,521
  • 8
  • 37
  • 74
  • 2
    The issue is that the `SessionFactory` is the `EntityManager`. and by exposing the `SessionFactory` as a bean spring boot will no longer bootstrap the `EntityMnagerFactory`. Resulting in a reference from the `SessionFcatory` to the `SessionFactory` (Because that is the entity manager). This is a result of the change in Hibernate 5.2. But why do you need a `SessionFactory`? Just use plain JPA instead of hibernate. – M. Deinum Aug 15 '18 at 11:18
  • @M. Deinum oh, that finally makes sense, thanks, man! I'm migrating an existing app at work and unfortunately I'm not a kind of person making such decisions yet. So I just have too. Do you have an idea for a solution for the issue? – Dmitry Senkovich Aug 15 '18 at 11:22
  • 1
    Downgrade hibernate to the same version as in Spring Boot 1.5. At least < 5.2. Or rewrite the dao's not to use the `SessionFactory` but rather an `EntityManager` and use `entityManager.unwrap(Session.class)` to obtain the `Session`. However if you start down that road you are probably better of moving to JPA all together. Or wait a couple of weeks (months?) for Spring Boot 5.1 and manually configure the `SessionFactory` as with that it can also function as the `EntityManagerFactory` but that requires Hibernate 5.3 and Spring 5.1. – M. Deinum Aug 15 '18 at 11:24
  • @M. Deinum not sure I understand you last idea. I mean I'm configuring it manually even now. The main problem I see that I would have to create a test database datasource myself, repeating all the default HSQLDB properties.. – Dmitry Senkovich Aug 15 '18 at 11:34
  • No you aren't configuring it manually. You are only unwrapping the preconfigured `EntityManagerFactory` that is not manually configuring the full `SessionFactory`. – M. Deinum Aug 15 '18 at 11:35
  • @M. Deinum yeah, but I'm configuring it myself in source code, not in tests. I mean I'm manually configuring LocalSessionFactoryBean in the application configuration. The problem that in tests I will have to as well. And it is not a problem with the app config, but with the test config I would like to use preconfigured one in order not to mess with the default HSQLDB properties – Dmitry Senkovich Aug 15 '18 at 11:38
  • No you aren't... You are relying on Spring Boot to fully configure the `EntityManagerFactory` (datasource, mappings, dialect etc. etc.) which you than `unwrap` to a `SessionFactory`. That is not manually configuring the `SessionFactory`. With manual configuration you would do everything yourself, set the dialect, datasource etc. etc. that is what I mean with configuring it yourself. – M. Deinum Aug 15 '18 at 11:40
  • @M. Deinum I do understand you, you don't see it in the example on GitHub, it was about the actual application configuration:) – Dmitry Senkovich Aug 15 '18 at 11:42

0 Answers0