This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with find() in jpa

 
chirag jain
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using JPA with hibernate. When I use



then change the entityObj by using its setters, and commit the JTA transaction , changed data is geting saved in table without calling persist() or merge().

Ex:

after this, data in table changes in column mapped to for field1.

What can be the problem?

 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hej,

welcome to the ranch!

This is how it is intended to work. merge is used when you want to reattach a detached object to the managed persistence context so it is irrelevant for your example. persist adds an unmanaged entity to the managed persistence context. Changes in managed entities are persisted when the transaction is committed.

See here for the JPA lifecycle.

If you want more explicit control then a framework like iBatis is for you.
 
chirag jain
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But here I am not calling persist() or merge(), then why the changes made to entity object are persisted in table? I have tried by closing entity manager, but still it does not work..



after this, data in table changes in column mapped to field1.

What is the solution??
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Commit the transaction before you change the field. Then the entity gets detached.

persist() and merge() are not simple "save" commands. They change the state of an entity regarding the current persistence context (Hibernate session). If the entity is managed, changes will be persisted once the transaction is committed. If the bean is detached, changes are lost, unless you merge the entity into the session - that is what merge() is for. If you get an entity from the entity manager (by find() or get()) it is managed.
 
chirag jain
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then is there any way to "detach" the entity from persistence context?
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In JPA 2.0 the EntityManager has a detach method for this. Before 2.0 you would just get an entitymanager, fetch the entity and close the entitymanager. (Or you could hack with serialisation... but that is not a good idea.)
 
chirag jain
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I have described in my previous post, I have tried by closing entity manager, but not working...

transaction.start();
entitymanager = createEntityManager();
entityObj = entitymanager.find();
closeEntityManager(entitymanager );
entityObj.setField1(value);
transaction.commit();
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
chirag jain wrote:As I have described in my previous post, I have tried by closing entity manager, but not working...

transaction.start();
entitymanager = createEntityManager();
entityObj = entitymanager.find();
closeEntityManager(entitymanager );
entityObj.setField1(value);
transaction.commit();


I don't know how your closeEntityManager method works. But you can't commit a transaction to a closed entity manager. And, if you are committing successfully, changes are persisted, as expected. Entity manager and transaction should be nested, the transaction within the entity manager.
 
chirag jain
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is my closeEntityManager() method:



Entity manager and transaction should be nested, the transaction within the entity manager.


This transaction is not hibernate transaction. This is JTA transaction and we are using it with EJB 2.0.


My jpa work is done inside session ejb method, so entity manager has to be nested in JTA transaction.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic