如何从javax.persistence.EntityManager获取org.hibernate.Query?

时间:2015-04-30 08:17:21

标签: java hibernate java-ee jpa

如何从 javax.persistence.EntityManager 对象获取 org.hibernate.Query (hibernate 4.0.1)

1 个答案:

答案 0 :(得分:4)

首先获得org.hibernate.Session

Session session = entityManager.unwrap(Session.class);

然后像往常一样使用session创建查询:

Query query = session.createQuery("from Stock where stockCode = :code ");