【问题标题】:using JPA2 for an entity update使用 JPA2 进行实体更新
【发布时间】:2012-03-15 23:20:59
【问题描述】:

我不太习惯 JPA...所以这更像是一个基本问题

我使用 JPA2 -(不是休眠)

我有这个功能,我想更新一下我的

@RequestScoped // (this is javax.faces.bean.RequestScoped)
@Stafeful // (this is javax.ejb.Stateful)
public class MyProvider {


    @Inject
    private EntityManager entityManager;

    /* some variables and getters and setters */

    public void setLocked(Long id, boolean locked) {
        entityManager.getTransaction().begin();
        user = userProvider.findUserByID(id);
        user.setLocked(locked);
        entityManager.persist(user);

        // i also tried it with refresh instead of persist
        entityManager.refresh(user);

        entityManager.getTransaction().commit();
    }
}

但此时我收到此错误 entityManager.getTransaction().begin();

java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()
    org.hibernate.ejb.AbstractEntityManagerImpl.getTransaction(AbstractEntityManagerImpl.java:996)
    org.jboss.as.jpa.container.AbstractEntityManager.getTransaction(AbstractEntityManager.java:498)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.jboss.weld.util.reflection.SecureReflections$13.work(SecureReflections.java:264)
    org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:52)
    org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInvocation(SecureReflectionAccess.java:137)
    org.jboss.weld.util.reflection.SecureReflections.invoke(SecureReflections.java:260)
    org.jboss.weld.bean.builtin.CallableMethodHandler.invoke(CallableMethodHandler.java:51)
    org.jboss.weld.bean.proxy.EnterpriseTargetBeanInstance.invoke(EnterpriseTargetBeanInstance.java:56)
    org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:105)
    org.jboss.weldx.persistence.EntityManager$-1570536921$Proxy$_$$_Weld$Proxy$.getTransaction(EntityManager$-1570536921$Proxy$_$$_Weld$Proxy$.java)
    de.demoapp.controller.UserController.setLocked(UserController.java:452)
    de.demoapp.controller.UserController.lock(UserController.java:721)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:601)
    org.apache.el.parser.AstValue.invoke(AstValue.java:262)
    org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
    org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:39)
    org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    javax.faces.component.UICommand.broadcast(UICommand.java:315)
    javax.faces.component.UIData.broadcast(UIData.java:1093)
    javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62)

我也读过 this 文章 - 但它并没有真正帮助,它仍然抛出:javax.persistence.TransactionRequiredException: no transaction is in progress

我不确定我是否做错了什么......

【问题讨论】:

    标签: jpa-2.0 java-ee-6 sql-update


    【解决方案1】:

    是的,有一些根本性的错误。您在容器管理的事务和用户管理的事务之间存在不匹配。 EntityManager.getTransaction 的文档非常清楚地说明了问题所在:

    投掷:
    IllegalStateException - 如果在 JTA 实体管理器上调用

    问题来自代码中的 entityManager.getTransaction(),使用 /flush/persist/etc 进行黑客攻击无关紧要,因为在执行这些行之前会发生异常。

    有关该主题的更多详细信息,请访问:http://en.wikibooks.org/wiki/Java_Persistence/Transactions

    【讨论】:

      【解决方案2】:

      好的,因为 Mikko 的真正好答案,我知道是对的:

      我导入:

      import javax.ejb.TransactionAttribute;
      import static javax.ejb.TransactionAttributeType.REQUIRED; // this one is important!!!
      

      我的注释现在是:

      @RequestScoped
      @Stateful
      @TransactionAttribute(value=REQUIRED)
      public class UserProvider {
      
          public void setLocked(Long id, boolean locked) {
              User user = new User();
              user = findUserByID(id);
              user.setLocked(locked);
              entityManager.merge(user);
              }
          }
      

      现在它可以工作了,但原因是第二次导入和注释!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-12
        • 2013-02-05
        • 2019-10-11
        • 2018-11-27
        • 1970-01-01
        相关资源
        最近更新 更多