I wanted to know what would be the ideal way to update a entity in Hibernate. For discussion sake, Lets have a entity Person
// 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.
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.