Spring5整合Hibernate5的版本适配问题和解决方法

针对的问题:

  • 使用hibernate5.4以及以上的版本整合spring5.1时,在IoC容器使用下面的类配置hibernate事务时会出现hibernate5.LocalSessionFactory的问题,Eclipse报的错误很含糊,但是也能找到一点线索,会有一行ClassNotDefFound错误。
  • 错误说明:就是两者的版本不适配
  • 这个问题最好的解决方法就是换成相称的版本,直接看下面解决方法吧。

解决方法:

请看这里源码的注释:


/**
 * {@link FactoryBean} that creates a Hibernate {@link SessionFactory}. This is the usual
 * way to set up a shared Hibernate SessionFactory in a Spring application context; the
 * SessionFactory can then be passed to data access objects via dependency injection.
 *
 * <p>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, {@code LocalSessionFactoryBean} is an immediate alternative
 * to {@link org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean} for common
 * JPA purposes: In particular with Hibernate 5.3, the Hibernate {@code SessionFactory}
 * will natively expose the JPA {@code EntityManagerFactory} interface as well, and
 * Hibernate {@code BeanContainer} integration will be registered out of the box.
 * In combination with {@link HibernateTransactionManager}, this naturally allows for
 * mixing JPA access code with native Hibernate access code within the same transaction.
 *

说明(翻译一下):

方法:

从spring5.1开始,将兼容hibernate5.0,5.1,5.2,5.3,但是一般建议你直接使用spring5.3的版本,这是推荐的最直接最好的选择,防止你出问题还怪我不好用

对于JPA也是一样,而且更香。

教训:以后再有问题,先看下源码可有注释,解决不了的再百度,英语好的去谷歌


版权声明:本文为aa10133原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。