Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassCastException in JpaHibernateConnectionProvider with spring 5.3 #1034

Closed
7erg opened this issue Nov 13, 2020 · 7 comments
Closed

ClassCastException in JpaHibernateConnectionProvider with spring 5.3 #1034

7erg opened this issue Nov 13, 2020 · 7 comments

Comments

@7erg
Copy link

7erg commented Nov 13, 2020

Exception:
java.lang.ClassCastException: class com.sun.proxy.$Proxy113 cannot be cast to class org.hibernate.internal.SessionImpl (com.sun.proxy.$Proxy113 and org.hibernate.internal.SessionImpl are in unnamed module of loader 'app')
at org.javers.spring.jpa.JpaHibernateConnectionProvider.getConnection(JpaHibernateConnectionProvider.java:22)

Here
(SessionImpl)entityManager.unwrap(Session.class);

entityManager.unwrap(Session.class) now returns Proxy instead of SessionImpl instance, because of changes in HibernateJpaVendorAdapter: entityManagerInterface changed from HibernateEntityManager (deprecated) to Session.

Probable fix: (SessionImpl)entityManager.unwrap(SessionImpl.class);

@temofey1989
Copy link

+1

@qanik1307
Copy link

Probably related: java.lang.ClassCastException: class com.sun.proxy.$Proxy185 cannot be cast to class org.hibernate.engine.spi.SessionFactoryImplementor (com.sun.proxy.$Proxy185 and org.hibernate.engine.spi.SessionFactoryImplementor are in unnamed module of loader 'app')
at org.javers.spring.boot.sql.JaversSqlAutoConfiguration.javersSqlDialectName(JaversSqlAutoConfiguration.java:55)

@temofey1989
Copy link

temofey1989 commented Nov 14, 2020

Unfortunately javersSqlDialectName bean has no @ConditionalOnMissingBean annotation. So, it hard to override this bean.

Also, the same problem with casting in the JpaHibernateConnectionProvider.

I'm using:

  • Spring Boot 2.4.0
  • Javers 5.13.2
  • Hibernate 5.4.23.Final

The workaround for me is:

  • exclude Javers autoconfiguration
    @SpringBootApplication(
        exclude = [JaversSqlAutoConfiguration::class]
    )
    
  • define a custom configuration with overrides:
    @Configuration
    class Config : JaversSqlAutoConfiguration() {
    
        @PersistenceContext
        lateinit var entityManager: EntityManager
    
        @Autowired
        lateinit var entityManagerFactory: EntityManagerFactory
    
        @Bean
        override fun javersSqlDialectName(): DialectName {
            val implementor = entityManagerFactory.unwrap(SessionFactoryImplementor::class.java)
            return DialectMapper().map(implementor.jdbcServices.dialect)
        }
    
        @Bean
        override fun jpaConnectionProvider(): ConnectionProvider {
            return ConnectionProvider {
                val session = (entityManager.unwrap(Session::class.java) as EntityManagerProxy).targetEntityManager as SessionImpl
                session.connection()
            }
        }
    }
    

I hope this would be useful for someone... 😃

@bartoszwalacik
Copy link
Member

Which version of Hibernate do you use?

@7erg
Copy link
Author

7erg commented Nov 14, 2020

Which version of Hibernate do you use?

5.4.23.Final

@bartoszwalacik
Copy link
Member

#1035

@bartoszwalacik
Copy link
Member

fixed in 5.14.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants