0

I am using openejb as my standalone container to run my unit testcases, in all the test in all the tests i was able to retrieve the values but not able to insert or update. Even if i try entitymanager.merge(obj), it is selecting the value correctly but it is not updating it.

I am new to this , so please help me with this error

thanks in advance

my persistence.xml file

 <persistence-unit name="test"
        transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>java:/OracleDS</jta-data-source>
.....entity classes.....
<properties>
            <property name="eclipselink.target-server" value="JBoss" />
            <property name="eclipselink.target-database" value="Oracle" />
            <property name="eclipselink.logging.level" value="FINE" />
            <property name="eclipselink.logging.parameters" value="true" />
        </properties>
    </persistence-unit>  

my dao method

tp.setname("van");
tp.settype("vehicle");
//entityManager.getTransaction().begin();
entityManager.merge(tp);
//entityManager.getTransaction().commit();
DataNucleus
  • 15,497
  • 3
  • 32
  • 37
user1996206
  • 98
  • 1
  • 10
  • There is no error which is displayed in the console . even my test case is passed . But when I check the DB the data is not inserted.In console the select query is displayed but not the insert query. – user1996206 Jan 21 '13 at 07:23
  • You can try `entityManager.flush()` after merge. – Nayan Wadekar Jan 21 '13 at 07:41
  • I have given a try for that also but it gives an exception as javax.ejb.EJBException: The bean encountered a non-application exception; nested exception is: javax.persistence.TransactionRequiredException: Exception Description: No transaction is currently active – user1996206 Jan 21 '13 at 09:09

2 Answers2

0

Do the followings for

Persist new entity:

Transport tp = new Transport();
tp.setname("van");
tp.settype("vehicle");
entityManager.getTransaction().begin();  
entityManager.persist(tp);
entityManager.getTransaction().commit();

Update existing entity:

Query qry = entityManager.createQuery("Select t from 
                                       Transport t where t.type ='vechicle'");
List list = qry.getResultList();
//assume result return single entity
Transport tp = list.get(0);
tp.setInspectionDone(new Date()) ;
entityManager.getTransaction().begin();
entityManager.merge(tp);
entityManager.getTransaction().commit();
vels4j
  • 11,208
  • 5
  • 38
  • 63
  • Thanks sakthivel. I solved the problem. The reason was that i used @persistencecontext annotation which will be managed by Jboss container. Since no server is created, no transaction is available for the DDL to execute. So we have to create the persistence object through EntityManagerFactory and we have to create the entitymanager.This will do the trick. – user1996206 Jan 21 '13 at 12:39
0

I think you need to ad @Transactional annotation in your class or method

sendon1982
  • 9,982
  • 61
  • 44