Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to safely update hibernate entity?

First to explain the context. I have backend Java (Spring/Hibernate) application that I access via JMS. I have client app (RESTfull) that I access via Url. I have a complex entity with more than one list (most of it are lazy) and this entity is composition of xy other entities.

Problem: Since I access it via URL, I create Java object in client app from URL parameters. I'm sending it to backend via JMS, but on the backEnd, I do not have Hibernate object, so I cant simple merge it.

I can go trough all that came from client like:

  • get hibernate object by id
  • check what is different
  • set new values
  • update

and repeat it for every composition entity, but I'm wondering if there is more elegant and "easy to maintain" way to update this entity with all changes.

I hope I explained it well. Thanks in advance!

like image 656
Djordje Ivanovic Avatar asked Jan 22 '14 12:01

Djordje Ivanovic


1 Answers

From your description it seem the steps you follow are right. The first(get object) and last(update object) steps are inevitable. The only place where you can optimize is the checking/setting part. For that you can write a generalized method which accepts two objects and compares them for changes using Reflection. This way it can be reused again.

Here is a sample code using reflection. Change it to suit your need

like image 113
Sandeep Avatar answered Nov 05 '22 10:11

Sandeep