• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the difference between EntityManager methods refresh()&find() and persist() & merge()

 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Could any body explain me the difference between these methods:

Refresh and find methods of EntityManager


I read that refresh method is used to refresh the entity state from Database and the database returns only what entity is holding now.So what does refresh mean here?
For me it looks like refresh and find method does the same thing retrieving entity from Database.


Persist and merge methods of EntityManager


I really dont understand the exact difference between these methods.One thing i came to know is that merge method cannot be applied to new entities those are does not exist in the database.

Thanks in Advance

 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We use find, when we don't have managed entity yet, we use refresh when want to refresh managed entity value from database.

Persist is for new entities, merge is for existing entities. Persist cannot be used with existing entities.
You can see more information at http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManager.html.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karnati Sudhakar

Please check your private messages regarding an important administrative matter.

-Andrew
 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kengkaj Sathianpantarit wrote:We use find, when we don't have managed entity yet, we use refresh when want to refresh managed entity value from database.

Persist is for new entities, merge is for existing entities. Persist cannot be used with existing entities.
You can see more information at http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManager.html.


Find method retrieves entity from Database by using the Primary key when we dont have entity in our hand.But we are using refresh method to get entity data from Database though we have Entity data.How can be it be that there are differences between Entity Data and Database record?
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karnati Sudhakar wrote:
Find method retrieves entity from Database by using the Primary key when we dont have entity in our hand.But we are using refresh method to get entity data from Database though we have Entity data.How can be it be that there are differences between Entity Data and Database record?

We can use refresh, if we make changes to Entity data, but we want to reset to prior state before the changes have been made (we have to call refresh before the transaction is committed of course).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic