aspose file tools*
The moose likes Object Relational Mapping and the fly likes Best way to update a Entity. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Best way to update a Entity." Watch "Best way to update a Entity." New topic
Author

Best way to update a Entity.

Arjun Abhishek
Ranch Hand

Joined: Jul 08, 2008
Posts: 57
Hi
I wanted to know what would be the ideal way to update a entity in Hibernate. For discussion sake, Lets have a entity Person

class Person
int id;
int age;
String name;
// has respective getters and setters.

Say if I want to update the instance of Peron, whos id is known, I currently follow these steps.

1. Create a new instance of the Person(newPerson), with new values assigned in the fields.
2. Load the old instance of the Person(oldPerson) based on the id
3. Compare value of each field, if they are different, I change the value in the instance of the oldPerson. // This is the painful step depending on the number of fields and type of the fields.
4. Again, save the oldPerson in the session and commit the transaction.


Please kindly let me know if there is a better way to do the same.

thanks
Arjun.

Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Why even do step 1? Why not just load the entity from the Hibernate Session, manipulate the entity, and then have the Hibernate Session persist the entity. That would save alot of steps.

So, why aren't you just loading the entity from Hibernate and manipulating it? No comparisons needed.

-Cameron McKenzie
Arjun Abhishek
Ranch Hand

Joined: Jul 08, 2008
Posts: 57
Say the object is coming from the UI layer. The object undergoes some changes in the UI layer. I need to persist the changes that it underwent in the UI layer. It is not in the hibernate session.

You are suggesting about the detached mode of the object. i will take a look at that topic once again.

thanks
Arjun.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

If you're simply sending the POJO to the UI layer, then the UI layer should be sending the same POJO back to you in the data layer. So, all you need to do is send the entity in for an update. That's the beauty of POJO based enterprise development.

Now, if you're doing some silly stuff like transforming the POJO into something else when you exchange it back and forth with the client, then perhaps all this data checking is needed. But if it's just proper POJO driven development, you should only need to pass the POJO in for an update.

What's wrong with doing just that?

-Cameron McKenzie
Reehan Lalkhanwar
Ranch Hand

Joined: Jun 16, 2007
Posts: 106
Use session.merge()


Thank you,
Reehan
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Reehan Lalkhan wrote:User session.merge()


Indeed.

-Cameron McKenzie
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

*ooops*
Arjun Abhishek
Ranch Hand

Joined: Jul 08, 2008
Posts: 57
Thanks for all your replies. Its just simply pojo based development.
I think these info should help me. I will try the merge or saveorUpdate method.

thanks
Arjun.
Arjun Abhishek
Ranch Hand

Joined: Jul 08, 2008
Posts: 57
Hi
Can you please have a look and comment on the usage of the example criteria post too. Link


thanks
Arjun.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best way to update a Entity.