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

"merge" and "persist" query

 
satish bodas
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am using JPA with underlying Hibernate
1 >
is the method "persist" to be used for both insert & update ?
are there any performance issues involved here ?

Scenario 1 ::
If I have a user record already in the db with uiserid = 100

If I create a new Java bean and set it the id = 100

I then try and use the method " persist "

I get an exception of " detached " object

So I will have to use the method "merge"

Looks like the method "merge" is updating the db .

why is the trace not shoowing any update statements when I fire "merge " ?

2 >
I again create a new Userbean and set its "name" attribute with some value
I then try and use the method " persist "

NOTE :: here I have'nt set the PK field with any value ( field is userId )

Now Hibernate does not crib and the persistence goes through fine

So why did the scenario 1 fail and not scenario 2 ?

what does hibernate do behind the scenes ?

3 >
I tried doing an insert as well as update using "merge"
Both insert as well as updated worked fine with no issues
so how do the two methods " persist" and "merge" compare ?
I know that merge is to be used for detached objects but if I use "merge" everywhere instead of
"persist" are their any issues ?

Thanks ,
~satish
[ July 21, 2008: Message edited by: satish bodas ]
 
Eric Nielsen
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In scenario one, you probably should be using
entityManager.find(SomeClass.class, 100)
to retrieve the object first, then modify it and call persist. And yes persist is the primary method used for both insert and update.

merge should only be used to associate a detached object with the current persistence context.
 
Mike Keith
author
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satish,

The persist() and merge() operations are different in a few key ways:

1) persist is always defined to throw an exception if you pass an existing object into it

2) if the instance that you are passing in is not already managed then persist causes the instance that you pass in to become managed, while merge creates a copy and returns that copy

3) persist is always used only for creating new objects, while merge can be used to create new objects or merge data into existing objects

In your scenerio one you are actually merging data into an existing object, so you should be using merge (or alternatively finding the existing object and modifying the managed instance)

In scenerio two you are leaving the PK empty and probably have a @GeneratedValue annotation on it, allowing the persistence provider to create a nwe PK value and thereby create a brand new entity.

HTH,
 
satish bodas
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Eric and Mike for the repsonse .

1) persist is always defined to throw an exception if you pass an existing object into it


I tried it and it silently updated the record .
Persistence provider is Hibernate

In scenerio two you are leaving the PK empty and probably have a @GeneratedValue annotation on it, allowing the persistence provider to create a nwe PK value and thereby create a brand new entity


You are correct

To answer a few of my own doubts ::
If I have a "managed/attached" entity and if I make a change in any of its attributes - Hibernate will guarantee to make these changes without any specific call
So here you dont need persist

In case of a "detached" object - there is no alternative than to use "merge"

so for detached object updation - use merge .

My only question / doubt is - when we use merge with detached objects - hibernate is able to detect that the object exists .

There can be two ways it does this detection ::
1 >
Fire an updatequery get the updatecount and determine if update went through fine or not

2 >
Fire a select query and based on the results determine answer

also ...... why dont we see any trace sqls in logs when we execute "merge" command ?

Thank you ,
~satish
 
Mike Keith
author
Ranch Hand
Posts: 304
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) persist is always defined to throw an exception if you pass an existing object into it

Sorry, I should not have said "always" because if the object is actually already managed in the persistence context then persist will ignore that object, so in your case the object was probably already managed.

Merge does not generally cause any SQL. That will not usually happen until commit time (or a flush call).
 
satish bodas
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic