EntityManager的SessionFactory引发异常 [英] SessionFactory from EntityManager throws exception

查看:79
本文介绍了EntityManager的SessionFactory引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过JPA的EntityManager通过以下几行获取Hibernate的SessionFactory:

I'm trying to get the Hibernate's SessionFactory from JPA's EntityManager with the following lines:

@PersistenceContext
EntityManager manager;

public SessionFactory getSessionFactory(){
    sessionFactory = manager.unwrap(SessionFactory.class);
}

但是它抛出了这个异常:

But it throws this exception:

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获取会话工厂,可以使用它来获取会话.
在会话中,您可以根据需要获取工厂的信息:

You can't use unwrap to get session factory, you can use it go get the session.
From the session, you can get the factory if you need:

public SessionFactory getSessionFactory(){
    Session session = manager.unwrap(Session.class);
    sessionFactory = session.getSessionFactory(); 
}

这篇关于EntityManager的SessionFactory引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆