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

Required information on session.merge()

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Can any body explain me what exactly the session.merge() does?
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bhavesh,

When we doing reattachment process in hibernet, we used session.merge(). By doing this all changes are merged as single one then sync with database.
 
Bhavesh Dak
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ramasamy,

Here is my scenario, I have to use same function twice one for saving an object with fresh data (data that is not in DB) and one for updating and object which will have data that is loaded from db and updated by application.

If i put only session.saveOrUpdate(object) I am getting NonUniqueObject Exception.

When I am calling session.merge(object) it is not updating data to db but executes without any exception.

So what should I need to do to make my function work for both saving and updating data.



 
Bhavesh Dak
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
session.merge() behaves very strange as I don't know how to use it correctly.

I am able to resolve my issue by manipulating relational properties of Objects and with the help of inverse property of hibernate.


Regards,
Bhavesh
 
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Merge() is used to re-connect an Entity with the persistence session.

As an example, assume you have an Entity class named User.

In your EJB, you create a new User:

User user = new User();

This object is not associated with the persistence unit yet.

You then set some values:

user.setFirstName("first name");

You then associate it with the persistence unit, using the persist() method:

context.persist(user);

Now, the persistence unit will manage the User object, saving it to the database when it is time (not necessarily right away).

Let's say you send this User object to the web client, so the web application can give it to the user to edit. This will disconnect it from the persistence unit. When the application sends the object back to the EJB, you need to re-associate the (currently) disconnected User object with the persistence unit. You do this using the context.merge(user) method.

When you call merge(), it will re-associate the object with the managed User object, and update the values to those passed in the user-edited User object.

At some later time, the persistence unit will take care of applying the changes to the database record.

So, persist() is used for associating new Entity objects with the persistence unit, and merge() is used for re-associating disconnected (but existing) Entity objects with the persistence unit.

By the way, this is all explained very well in the book: EJB3 in Action from Manning Publications.

 
Bhavesh Dak
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark E Hansen for information

I will surely do some practicals on it.

Right now I have found my way out to the problems but I am very much curious to use these fundamentals in my projects.

Thanks again ..
 
Remember to always leap before you look. But always take the time to smell the tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic