• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with find() in jpa

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
then is there any way to "detach" the entity from persistence context?
 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic