Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can be update the data without using em.merge()?

 
dinesh thalis
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to update my table record using OracleToplink persistent provider.

In the session bean i have put my code as below;



its working properly.



But my problem is how EJB container update data without using em.persist() method ???

Is there any implicit entity persisting concept in EJB 3 ?

 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The persist() method in JPA is only for new objects. It is not even always required for new objects, if you have an existing object that references a new object it will automatically get persisted (provided relationship is cascading).

Nothing is required for updates. Any object read through the JPA EntityManager will automatically be tracked in the transaction context for changes, and the changes will be committed at the end of the transaction, on when flush() is called. The exception is if you have a serialized or detached object, in which case you need to merge its changes into the managed object using merge().

 
dinesh thalis
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I edit data using my way(without merge) it is good method or not ? if I use this way what sort of problem I have to face? (any reference link)
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is normally much better to edit the managed objects directly and not use merge. Merge should only be used if you need to serialize objects to and from a remote client or web tier.

One thing you should be careful on though is your object's version field if using optimistic locking. If you read the object in one transaction, and send it to a client or web tier, then update it in another transaction, then you need to ensure you set the version of the object to the original one read. Otherwise their is little point to locking.
 
xsunil kumar
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you James, em.merge is only required if entity is not available in persistence context or we are using application managed transation. If you are using container managed trransation, then only required thing to set the other values. Entity manager will take care of updating data. If you are using application managed transation, then first you need to bring that object in persistence context ( if not available) by using find method and then update the field values by setter method, comit the transation. Value will be reflected into database.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic