Not able to update records

#1

Hi,

 

I am tring to update the simple object by using Update query by i am getting following exception

[ObjectDB 2.4.4_15] javax.persistence.TransactionRequiredException
Attempt to run update query when no transaction is active (error 611)
at com.objectdb.jpa.JpaQuery.executeUpdate(JpaQuery.java:758)

Following is the code i am executing

 

EntityManagerFactory emFactory = Persistence.createEntityManagerFactory(Constants.DATABASE_FILE_CLIENTSERVER_MODE);
  EntityManager em = emFactory.createEntityManager();
  Query query = em.createQuery(createUpdateQuery(userCredential, sessionID));
  int updateCount = query.setParameter("value", userCredential.getRegisteredEmailD()).executeUpdate();

 

And called method is as follows

private String createUpdateQuery(UserCredential trip, String sessionID){
  StringBuffer sqlQuery = new StringBuffer("UPDATE UserCredential p ");
  sqlQuery.append(" SET sessionID='"+(sessionID)+"'");
  sqlQuery.append(" WHERE 1=1 AND registeredEmailD = ':value'");
  return sqlQuery.toString();
}

 

Kindly provide your views on the same.

#2

Here Constants.DATABASE_FILE_CLIENTSERVER_MODE is the connection URL.

#3

As per my knowledge I have already tried all known things.

Waiting for reply. I am just stucked here.

#4

I think you have to open a transaction on your own.

Try surrounding your executeUpdate with

em.getTransaction().begin() to start the transaction and

em.getTransaction().commit() to end the transaction wirh a commit.

#5

It schould then look like:

EntityManagerFactory emFactory = Persistence.createEntityManagerFactory(Constants.DATABASE_FILE_CLIENTSERVER_MODE);
EntityManager em = emFactory.createEntityManager();

em.getTransaction().begin(); // start the transaction
Query query = em.createQuery(createUpdateQuery(userCredential, sessionID));
int updateCount = query.setParameter("value", userCredential.getRegisteredEmailD()).executeUpdate();
em.getTransaction().commit(); // end the transaction

On an error you had propably to rollback your transaction, to discard all changes made to the database.

 

#6

Thanks,

It has updated data correctly.

Reply