-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: JPA JSE desktop application JUnit (Transaction problem?)
PostPosted: Thu Apr 24, 2014 4:21 am 
Beginner
Beginner

Joined: Sat Feb 16, 2008 3:09 pm
Posts: 24
Hello I have a JSE (desktop) application which uses JPA.
When I try to JUnit Test it, I run into a strange problem.
Basically I have two Entities which extend a superclass.

Code:
@MappedSuperclass
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Schluesselliste implements Serializable {
   

   @Id @GeneratedValue
   private Long id;
   
   @Column(unique=true)
   private int nr;

@Entity
public class Rechtsvorgang extends Schluesselliste {

@Entity
public class TatbestandArt extends Schluesselliste


I need to test that the attribute nr is unique for each subclass, but that same values are allowed between subclasses.
I think this works, but my JUnit test behaves strangely:

Code:
public class SchluesselisteTest {
   
   private static RechtsvorgangDAO rechtsvorgangDAO;
   private static TatbestandArtDAO tatbestandArtDAO;
   
   @BeforeClass
   public static void setup() {
      EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
      EntityManager em = emf.createEntityManager();
      rechtsvorgangDAO = new RechtsvorgangDAOBean(em);
      tatbestandArtDAO = new TatbestandArtDAOBean(em);
   }
   
   @Test
   public void test() {
      assertNotNull(rechtsvorgangDAO);
      assertNotNull(tatbestandArtDAO);
   }
   
   
   @Before
   public void cleanUp() {
      rechtsvorgangDAO.deleteAll();
      tatbestandArtDAO.deleteAll();
   }
   
   
   @Test
   public void testSelbeNrInVerschiedenenSchluessellisten() {
      Rechtsvorgang rechtsvorgang = new Rechtsvorgang();
      rechtsvorgang.setNr(1);
      rechtsvorgangDAO.save(rechtsvorgang);
      TatbestandArt tatbestandArt = new TatbestandArt();
      tatbestandArt.setNr(1);
      tatbestandArtDAO.save(tatbestandArt);
      Rechtsvorgang rechtsvorgangP = rechtsvorgangDAO.findByNr(1);
      assertNotNull(rechtsvorgangP);
      TatbestandArt tatbestandArtP = tatbestandArtDAO.findByNr(1);
      assertNotNull(tatbestandArtP);
   }
   
   @Test(expected=RollbackException.class)
   public void testUniqueNr() {
      Rechtsvorgang rechtsvorgang = new Rechtsvorgang();
      rechtsvorgang.setNr(1);
      rechtsvorgangDAO.save(rechtsvorgang);
      Rechtsvorgang rechtsvorgang2 = new Rechtsvorgang();
      rechtsvorgang2.setNr(1);
      rechtsvorgangDAO.save(rechtsvorgang2);
      
   }

}

When I run the tests in isolation, each test succeeds. But when I run the whole class a Junit Test following happens:
- cleanUp() is called
- testUniqueNr() is executed and succeeds.
- cleanUp() is called and a Exception is thrown:
javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: ORA-00001: unique constraint (TEST.SYS_C0013343) violated

at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1361)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1289)

So the problem occurs when I run cleanUp after testUniqueNr where the exeption is excpected.

relevant code:
Code:
public class RechtsvorgangDAOBean implements RechtsvorgangDAO {
   
   private EntityManager entityManager;
   
   

   public RechtsvorgangDAOBean(EntityManager entityManager) {
      super();
      this.entityManager = entityManager;
   }

   public void save(Rechtsvorgang rechtsvorgang) {
      entityManager.getTransaction().begin();
       entityManager.persist(rechtsvorgang);
       entityManager.getTransaction().commit();
   }

   

   public void deleteAll() {
      entityManager.getTransaction().begin();
      Query query = entityManager.createNamedQuery(Rechtsvorgang.DELETE_ALL);
      query.executeUpdate();
      entityManager.getTransaction().commit();

   }
}

@Entity
@NamedQueries({
   @NamedQuery(name="Rechtsvorgang.findAll", query="SELECT r from Rechtsvorgang r ORDER BY r.nr"),
   @NamedQuery(name="Rechtsvorgang.deleteAll", query="DELETE FROM Rechtsvorgang r"),
   @NamedQuery(name="Rechtsvorgang.findByNr", query="SELECT r from Rechtsvorgang r WHERE r.nr = :nr")
})
public class Rechtsvorgang extends Schluesselliste {


Thanks for any input.

Can anyone recommend a good resource for using jpa in jse applications?

From my pom.xml:
Code:
<dependencies>
      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.11</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>dbunit</groupId>
         <artifactId>dbunit</artifactId>
         <version>2.2</version>
      </dependency>
      <dependency>
         <groupId>com.oracle</groupId>
         <artifactId>ojdbc6</artifactId>
         <version>11.1.0.7.0</version>
      </dependency>
      <dependency>
         <groupId>org.hibernate.javax.persistence</groupId>
         <artifactId>hibernate-jpa-2.1-api</artifactId>
         <version>1.0.0.Final</version>
      </dependency>
      <dependency>
         <groupId>org.hibernate</groupId>
         <artifactId>hibernate-entitymanager</artifactId>
         <version>4.0.1.Final</version>
      </dependency>
   </dependencies>


Top
 Profile  
 
 Post subject: Re: JPA JSE desktop application JUnit (Transaction problem?)
PostPosted: Thu Apr 24, 2014 4:32 am 
Beginner
Beginner

Joined: Sat Feb 16, 2008 3:09 pm
Posts: 24
Follow up question:

Is it advisable to create the EntityManager once for the application (like I do it)?
I see other examples which create only the EntityManagerFactory centrally and instantiate the EntityManager in every method which needs it.


Top
 Profile  
 
 Post subject: Re: JPA JSE desktop application JUnit (Transaction problem?)
PostPosted: Thu Apr 24, 2014 9:07 am 
Beginner
Beginner

Joined: Sat Feb 16, 2008 3:09 pm
Posts: 24
Ok I have tried to alter the deleteAll() Method according to the idiom from "Java Persistence with Hibernate"

Code:
public void deleteAll() throws WasserRechtPersistenceException {
      
      EntityManager em = null;
      EntityTransaction tx = null;
      try {
         em = emf.createEntityManager();
         tx = em.getTransaction();
         tx.begin();
         Query query = entityManager.createNamedQuery(Rechtsvorgang.DELETE_ALL);
         query.executeUpdate();
         tx.commit();
      }
      catch (RuntimeException ex) {
         log.error("Error trying to delete all Rechtsvorgang", ex);
         try {
            tx.rollback();
         } catch (RuntimeException ex2) {
            log.error("Unable to rollback Tx", ex2);
         }
      } finally {
         em.close();
      }
      

   }


Code:
@NamedQuery(name="Rechtsvorgang.deleteAll", query="DELETE FROM Rechtsvorgang r"),


When I debug the code (on an empty database) I see that after tx.begin()
tx is not null and tx.tx is an JDBCTransaction
with query.executeUpdate();
however I get a
javax.persistence.TransactionRequiredException: Executing an update/delete query
at org.hibernate.ejb.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:96)
at de.bwl.uis.module.wasserrecht.persistence.RechtsvorgangDAOBean.deleteAll(RechtsvorgangDAOBean.java:67)
at de.bwl.uis.module.wasserrecht.persistence.test.RechtsvorgangDAOSimpleTest.test(RechtsvorgangDAOSimpleTest.java:25)
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:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Whats wrong???????????? :-(


Top
 Profile  
 
 Post subject: Re: JPA JSE desktop application JUnit (Transaction problem?)
PostPosted: Thu Apr 24, 2014 9:22 am 
Beginner
Beginner

Joined: Sat Feb 16, 2008 3:09 pm
Posts: 24
Ok the last mistake was a stupid copy and paste error.
I used two different instances of the EntityManager in the method.
After correcting that all unit test succeed.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.