javax.transaction.RollbackException: Transaction marked for rollback. No useful information, no reason WHY it's marked for rollback. An alternative way to achieve that useless error message would be: tx.begin(); Thing t = em.find(Thing.class,thingId); t.changeField("new value"); em.persist(new SubThing(t); tx.commit(); What I gather is that for some reason it doesn't like it when the following conditions happen to Entity e within a transaction: 1) e has a field which is modified 2) e has a collection of other entities which gains a new entity (perhaps when one is modified too?) 3) the entity of which there is a collection within e has a reference to e It's totally fine if you only change the field, or only add the entity to the collection. Another question, are there known differences when using JPA with hibernate as the provider within Glassfish? All the tutorials I've read, and the Manning Java Persistence with Hibernate book use the following to get a reference to an EntityManager: EntityManagerFactory emf = Persistence.createEntityManagerFactory("nameOfPersistenceUnit"); EntityManager em = emf.createEntityManager(); But from the very beginning of my new project (using Hibernate as a JPA provider in Glassfish) that threw an exception every time I tried it. They also all get a hold of a transaction with: EntityTransaction tx = em.getTransaction(); tx.begin(); But every time I've tried that, I get an exception saying: java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()

Oracle Community | rwillie6 | 9 years ago
tip
Click on the to mark the solution that helps you, Samebug will learn from it.
As a community member, you’ll be rewarded for you help.
  1. 0

    Cannot modify entity and collection within entity in same transaction

    Oracle Community | 9 years ago | rwillie6
    javax.transaction.RollbackException: Transaction marked for rollback. No useful information, no reason WHY it's marked for rollback. An alternative way to achieve that useless error message would be: tx.begin(); Thing t = em.find(Thing.class,thingId); t.changeField("new value"); em.persist(new SubThing(t); tx.commit(); What I gather is that for some reason it doesn't like it when the following conditions happen to Entity e within a transaction: 1) e has a field which is modified 2) e has a collection of other entities which gains a new entity (perhaps when one is modified too?) 3) the entity of which there is a collection within e has a reference to e It's totally fine if you only change the field, or only add the entity to the collection. Another question, are there known differences when using JPA with hibernate as the provider within Glassfish? All the tutorials I've read, and the Manning Java Persistence with Hibernate book use the following to get a reference to an EntityManager: EntityManagerFactory emf = Persistence.createEntityManagerFactory("nameOfPersistenceUnit"); EntityManager em = emf.createEntityManager(); But from the very beginning of my new project (using Hibernate as a JPA provider in Glassfish) that threw an exception every time I tried it. They also all get a hold of a transaction with: EntityTransaction tx = em.getTransaction(); tx.begin(); But every time I've tried that, I get an exception saying: java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()

    Root Cause Analysis

    1. javax.transaction.RollbackException

      Transaction marked for rollback. No useful information, no reason WHY it's marked for rollback. An alternative way to achieve that useless error message would be: tx.begin(); Thing t = em.find(Thing.class,thingId); t.changeField("new value"); em.persist(new SubThing(t); tx.commit(); What I gather is that for some reason it doesn't like it when the following conditions happen to Entity e within a transaction: 1) e has a field which is modified 2) e has a collection of other entities which gains a new entity (perhaps when one is modified too?) 3) the entity of which there is a collection within e has a reference to e It's totally fine if you only change the field, or only add the entity to the collection. Another question, are there known differences when using JPA with hibernate as the provider within Glassfish? All the tutorials I've read, and the Manning Java Persistence with Hibernate book use the following to get a reference to an EntityManager: EntityManagerFactory emf = Persistence.createEntityManagerFactory("nameOfPersistenceUnit"); EntityManager em = emf.createEntityManager(); But from the very beginning of my new project (using Hibernate as a JPA provider in Glassfish) that threw an exception every time I tried it. They also all get a hold of a transaction with: EntityTransaction tx = em.getTransaction(); tx.begin(); But every time I've tried that, I get an exception saying: java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction()

      at org.hibernate.ejb.AbstractEntityManagerImpl.getTransaction()
    2. Hibernate EJB
      AbstractEntityManagerImpl.getTransaction
      1. org.hibernate.ejb.AbstractEntityManagerImpl.getTransaction(AbstractEntityManagerImpl.java:317)
      1 frame
    3. Common Utilities
      EntityManagerWrapper.getTransaction
      1. com.sun.enterprise.util.EntityManagerWrapper.getTransaction(EntityManagerWrapper.java:565)
      1 frame
    4. com.stag.servlet
      AdminServlet.doPost
      1. com.stag.servlet.AdminServlet.doPost(AdminServlet.java:156)
      1 frame
    5. JavaServlet
      HttpServlet.service
      1. javax.servlet.http.HttpServlet.service(HttpServlet.java:738)
      2. javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
      2 frames
    6. Glassfish Core
      StandardPipeline.doInvoke
      1. org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:411)
      2. org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:290)
      3. org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:271)
      4. org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:202)
      5. org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632)
      6. org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577)
      6 frames
    7. Glassfish
      WebPipeline.invoke
      1. com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94)
      1 frame
    8. Glassfish Core
      ContainerBase.invoke
      1. org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:206)
      2. org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632)
      3. org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577)
      4. org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:571)
      5. org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1080)
      6. org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:150)
      7. org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:632)
      8. org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:577)
      9. org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:571)
      10. org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1080)
      10 frames
    9. org.apache.coyote
      CoyoteAdapter.service
      1. org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:270)
      1 frame
    10. Glassfish Core
      TaskBase.run
      1. com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:637)
      2. com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:568)
      3. com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:813)
      4. com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:339)
      5. com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:261)
      6. com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:212)
      7. com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265)
      7 frames
    11. Grizzly HTTP
      SSLWorkerThread.run
      1. com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106)
      1 frame