Spring 4 + Hibernate 5 = org.springframework.orm.jpa.EntityManagerHolder无法强制转换为org.springframework.orm.hibernate5.SessionHolder
好吧,在开始一个新的java项目并集成spring / hibernate等工具时,我是一个完全的初学者。事实上,这是我第一次这样做。 所以我相信这些错误对你们来说是显而易见的。
Guessings:
- 我期待的会议工厂不是spring注入的工厂。
- 错误的依赖关系。
错误
[2016-07-28 01:29:14.869] boot - 22234 ERROR [http-nio-8080-exec-1] --- [dispatcherServlet]: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: org.springframework.orm.jpa.EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder] with root cause java.lang.ClassCastException: org.springframework.orm.jpa.EntityManagerHolder cannot be cast to org.springframework.orm.hibernate5.SessionHolder at org.springframework.orm.hibernate5.HibernateTransactionManager.doGetTransaction(HibernateTransactionManager.java:376)
的pom.xml
4.0.0 org.pse plataforma-ejercicios 1.0-SNAPSHOT 1.8 4.3.1.RELEASE 1.3.5.RELEASE 5.2.0.Final 5.1.38 0.9.1.2 c3p0 c3p0 ${c3p0.version} org.springframework spring-orm ${org.springframework.version} org.hibernate hibernate-core 5.2.1.Final org.hibernate hibernate-validator 5.1.3.Final org.hibernate.common hibernate-commons-annotations 5.0.1.Final org.hibernate.javax.persistence hibernate-jpa-2.0-api 1.0.1.Final org.hibernate hibernate-entitymanager 5.1.0.Final org.mariadb.jdbc mariadb-java-client 1.1.7 org.springframework spring-core ${org.springframework.version} commons-logging commons-logging .... ...
基的context.xml
冬眠-的context.xml
true
hibernate.properties
hibernate.dialect org.hibernate.dialect.MySQL5Dialect hibernate.connection.driver_class org.mariadb.jdbc.Driver hibernate.connection.url jdbc:mariadb://127.0.0.1:3306/pse hibernate.connection.username pse hibernate.connection.password passpse hibernate.connection.min_pool_size 2 hibernate.connection.max_pool_size 10 hibernate.connection.autocommit true hibernate.hbm2ddl.auto update hibernate.show_sql true
StudentController.java
@RestController public class StudentController { private Logger logger = LoggerFactory.getLogger(StudentController.class); private StudentService studentService; @RequestMapping(value = "/student/{studentId}/classes", method = RequestMethod.GET) public ClassDTO getClassesForStudent(){ return new ClassDTO(); } @RequestMapping("/saveStudent/{nombre}/{apellido}") public void saveStudent(@PathVariable String nombre, @PathVariable String apellido) { logger.info("Entrada al controller de: TestHibernate"); Student student = new Student(); student.setLastName(apellido); student.setName(nombre); studentService.save(student); } @Autowired public void setStudentService(StudentService studentService) { this.studentService = studentService; } }
StudentServiceImpl.java
@Service public class StudentServiceImpl implements StudentService { private StudentDAO studentDAO; @Override @Transactional(readOnly = false) public void save(Student student) { studentDAO.save(student); } @Autowired public void setStudentDAO(StudentDAO studentDAO) { this.studentDAO = studentDAO; } }
StudentDAO.java
public class StudentDAO { private SessionFactory sessionFactory; public void save(Student student){ try { Session session = sessionFactory.getCurrentSession(); session.save(student); } catch (Exception e) { e.printStackTrace(); } } @Autowired public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } }
你使用Spring-Boot吗? 是这样,在我的情况下问题是@EnableAutoConfiguration – 使用exclude = HibernateJpaAutoConfiguration.class
- 在数据库模式中hibernate二级缓存和ON DELETE CASCADE
- 如何在spring / hibernate / jpa中创建自动表创建?
- JPA使用替代“persistence.xml”
- javax.persistence.PersistenceException:未找到类或包
- 如何将Hibernate 5安装到Apache Karaf v4中
- 我不能在我的代码中使用findOne()方法
- 没有可用于当前线程的实际事务的EntityManager – 无法可靠地处理“刷新”调用
- Hibernate在EntityTuplizerFactory.constructTuplizer中失败并出现NullPointerException? 为什么?
- Crudrepository /从其他表到实体的单个字段作为只读