Joergi
Joergi

Reputation: 1593

using JPA2 for an entity update

I'm not really used to JPA... so this is more like a basic question

I use JPA2 - (not hibernate)

I have this function, and I want to make an update on my

@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();
    }
}

but i get this error at this point 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)

I have also read this article - but it doesn't really help, it still throws: javax.persistence.TransactionRequiredException: no transaction is in progress

I'm not sure if I do something essential wrong...

Upvotes: 2

Views: 3540

Answers (2)

Joergi
Joergi

Reputation: 1593

okay, because of the the real good answer of Mikko, i do it know right:

I import:

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

my Annotations are now:

@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);
        }
    }

now it works, but the reason is the 2nd import and the anotation!

Upvotes: 1

Mikko Maunu
Mikko Maunu

Reputation: 42074

Yes, there is something essential wrong. You have mismatch between container managed transactions and user managed transactions. Documentation for EntityManager.getTransaction tells quite clearly what is the problem:

Throws:
IllegalStateException - if invoked on a JTA entity manager

Problem comes from entityManager.getTransaction() in your code, hacking with /flush/persist/etc does not matter because exception occurs before those lines are executed.

More details about the subject can be found from: http://en.wikibooks.org/wiki/Java_Persistence/Transactions

Upvotes: 3

Related Questions