我尝试使用以下几行从 JPA 的 EntityManager 获取 Hibernate 的 SessionFactory:
@PersistenceContext
EntityManager manager;
public SessionFactory getSessionFactory(){
sessionFactory = manager.unwrap(SessionFactory.class);
}
但它抛出了这个异常:
org.springframework.orm.jpa.JpaSystemException: Hibernate cannot unwrap interface org.hibernate.SessionFactory; nested exception is javax.persistence.PersistenceException: Hibernate cannot unwrap interface org.hibernate.SessionFactory
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:418)
...
Caused by: javax.persistence.PersistenceException: Hibernate cannot unwrap interface org.hibernate.SessionFactory
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.unwrap(AbstractEntityManagerImpl.java:1489)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
这可能是什么原因?
提前致谢
你不能使用unwrap
获取sessionfactory,可以使用它来获取session。
如果需要,您可以从会话中获取工厂:
public SessionFactory getSessionFactory(){
Session session = manager.unwrap(Session.class);
sessionFactory = session.getSessionFactory();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)