Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best way to update a Entity.

 
Arjun Abhishek
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use session.merge()
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reehan Lalkhan wrote:User session.merge()


Indeed.

-Cameron McKenzie
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*ooops*
 
Arjun Abhishek
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Can you please have a look and comment on the usage of the example criteria post too. Link


thanks
Arjun.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic