• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

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

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

 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic