This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes problem with find() in jpa Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "problem with find() in jpa" Watch "problem with find() in jpa" New topic
Author

problem with find() in jpa

chirag jain
Greenhorn

Joined: Oct 08, 2009
Posts: 6
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

Joined: Nov 18, 2008
Posts: 433
    
    2
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

Joined: Oct 08, 2009
Posts: 6
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

Joined: Nov 18, 2008
Posts: 433
    
    2
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

Joined: Oct 08, 2009
Posts: 6
then is there any way to "detach" the entity from persistence context?
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
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

Joined: Oct 08, 2009
Posts: 6
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

Joined: Nov 18, 2008
Posts: 433
    
    2
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

Joined: Oct 08, 2009
Posts: 6
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
 
subject: problem with find() in jpa
 
Similar Threads
Best mapping approach for reference/LOV/lookup data
[JPA] Dynamic CreateNativeQuery - getColumnNames (or ResultSet)?
Merge detached sub-class entity (JPA InheritanceType.SINGLE_TABLE) org.hibernate.WrongClassException
Cannot insert null into a non nullable column
JPA (TopLink) DB2 table generation