我试图从注入的EntityManager中解开Hibernate Session,因为我需要使用Hibernate的原生Criteria API.
当我尝试使用Criteria时,我得到以下异常:
Caused by: java.lang.IllegalStateException: No transactional EntityManager available
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:224)
at com.sun.proxy.$Proxy28.unwrap(Unknown Source)
at sk.uniba.ais2.fajr.dao.impl.PouzivatelDAOHibernate.findByLogin(PouzivatelDAOHibernate.java:22)
at sk.uniba.ais2.fajr.bo.PouzivatelService.findByLogin(PouzivatelService.java:20)
at sk.uniba.ais2.fajr.mvc.controller.FooController.getFoooFOO(FooController.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
... 62 more
但是,当我使用EntityManager本身的Query api时,一切正常.
我的配置:
db.xml
sql" value="true" />
factorybean">
PouzivatelDAOHibernate不起作用:
@Repository
public class PouzivatelDAOHibernate implements PouzivatelDAO {
@PersistenceContext
private EntityManager entityManager;
@Override
public Pouzivatel findByLogin(String login) {
Criteria criteria = entityManager.unwrap(Session.class).createCriteria(Pouzivatel.class);
return (Pouzivatel) criteria.add(Restrictions.eq("login",login)).uniqueResult();
}
}
PouzivatelDAOHibernate哪个有效:
@Repository
public class PouzivatelDAOHibernate implements PouzivatelDAO {
@PersistenceContext
private EntityManager entityManager;
@Override
public Pouzivatel findByLogin(String login) {
// Criteria criteria = entityManager.unwrap(Session.class).createCriteria(Pouzivatel.class);
// return (Pouzivatel) criteria.add(Restrictions.eq("login",login)).uniqueResult();
Query query = entityManager.createQuery("select p from Pouzivatel p where login=?");
query.setParameter(1,login);
return (Pouzivatel) query.getSingleResult();
}
}
编辑:我正在使用Spring Data JPA
最佳答案
原文链接:https://www.f2er.com/spring/432043.html