Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save List changes with Hibernate

I have an Object named Token. it has id, name, and value. After saving some data to db, I have loaded them into a web page

_____________________________________________
|____name____|____value____|____operation____|
   tkn1          10               ×
   tkn2          20               ×

the × sign enable me to delete a token from server collection now. I have added token tkn3 with value 30 and deleted tkn2 so the table would be:

_____________________________________________
|____name____|____value____|____operation____|
   tkn1          10               ×
   tkn3          30               ×

With these changes to the collection, how can I reflect them into database how to determine the records that deleted, and the records that added?

I applied tow solutions:

  1. I have compared -in business logic layer- the old data with the new data and find the differences between the then send to database two lists, the first contains the added tokens and the second contains the ids of tokens to be deleted.
  2. I added a flag named status to the object.. when I add the flag is NEW when I delete I just set flag to DELETE, and in DB layer I iterate over the collection one by one object and check the flag.. if NEW then add the record, if DELETE , delete it and if SAVED (no changes) I do no changes to it..

My questions:

  • Is this way is good to do this task..?
  • Is there a Pattern to accomplish this task?
  • Can Hibernate help me to do that?
like image 872
JDurrah Avatar asked Dec 07 '25 13:12

JDurrah


1 Answers

• Is this way is good to do this task..? NO

• Is there a Pattern to accomplish this task? YES

• Can Hibernate help me to do that? Hibernate provides the solution for such situation using Cascade Attribute for List property

Refer http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/collections.html

http://www.mkyong.com/hibernate/hibernate-cascade-example-save-update-delete-and-delete-orphan/

like image 130
Rahul Agrawal Avatar answered Dec 10 '25 02:12

Rahul Agrawal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!