java – 没有事务性的EntityManager可用 – 使用JPA Api,Hibernate Session出错

前端之家收集整理的这篇文章主要介绍了java – 没有事务性的EntityManager可用 – 使用JPA Api,Hibernate Session出错前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我试图从注入的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

最佳答案
我实际上解决了这个问题(解决方案非常简单):使用org.springframework.transaction.annotation.Transactional而不是javax.transaction.Transactional

猜你在找的Spring相关文章