aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Managed entity not synchronised with DB? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Managed entity not synchronised with DB?" Watch "Managed entity not synchronised with DB?" New topic
Author

Managed entity not synchronised with DB?

John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
I came across this paragraph in "EJB 3 in action"



Isn't entity managed after line 'entityManager.persist(item);'?
If it is managed, it should automatically synchronised with db, right?
Then why is there need to call 'refresh'?
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Being a managed entity does not automatically ensure that the entity is refreshed from DB. Quoting from the ejb spec:

3.2.3 Synchronization to the Database
The state of persistent entities is synchronized to the database at transaction commit. This synchronization involving writing to the database any updates to persistent entities and their relationships as specified
above.An update to the state of an entity includes both the assignment of a new value to a persistent property or field of the entity as well as the modification of a mutable value of a persistent property or field.
Synchronization to the database does not involve a refresh of any managed entities unless the refresh operation is explicitly invoked on those entities


Thanks and Regards
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
Thanks, I thought it works automatically in both ways.
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
I was trying this out practically. Strangely it turns out that the managed is indeed refreshed from the DB after the persist. Here is the code:



And here is the output:


The output suggests that Cust is refreshed with the updated city after it is persisted. I am confused here :confused
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Managed entity not synchronised with DB?
 
Similar Threads
Query on Detached and Removed Entities
EJB3 in Action errata for EntityManager.refresh() method
EntityManager
Entitymanager.refresh doubt
EJB 3 IN ACTION - EntityManager merge() and refresh() method conflict