Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to update master detail record using hibernate???

 
sajjad ahmad
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
i am using hibernate framwork to persist my java objects....i have a case where i have to update a master detail record

master record =======> CustomerInfo
detail records =======> CustomerAccounts

The problem is when i need to udate a record in detail section and another record to be added first time i just add those records one to be updated with it's PK and other to be added without it's Pk (as i have configured sequance for this table in appropriate xml file) t the set and add it to master customerInfo object and try to update it nothing happens in detail table i.e neither record is updated or saved i am confused about it .....am i going in wrong direction???let me also give you the code snipet i am using

after searching the customerInfo object i write following code

CustomerAccount customerAccount1 = new CustomerAccount();
customerAccount1.setAccountNo("268312");
customerAccount1.setBankName("dasdsja");
customerAccount1.setBranchName("dgajdgsja");

CustomerAccount customerAccount2 = new CustomerAccount();
customerAccount2.setAccountId(new Long("21"));
customerAccount2.setAccountNo("268312");
customerAccount2.setBankName("Habib Bank");
customerAccount2.setBranchName("BahadurAbad");

Set a = new HashSet();
a.add(customerAccount1);
a.add(customerAccount2);
customerInfo.setCustomerAccounts(a);
session.update(customerInfo);
see its very simple code i am not sure where i am getting wrong can anybosy tell me what else i have to do here
thanks in advance.....
sajjad ahmed paracha
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you in a transaction? Have you called commitTransaction()?

Hibernate won't execute the sql until you commit. Or you can explictly call session.flush();

Also, you should avoid overwriting the set an entity that you recieved from a call to get(). Hibernate uses a custom Set class that stores state data. (That way Hibernate knows which rows to delete if you remove something from the set.) If you overwrite the set by calling customerInfo.setCustomerAccounts(), you will be destroying the information.

Instead, retrieve the set from the entity and modify it:

Set a = customerInfo.getCustomerAccounts();
a.add(customerAccount1);
a.add(customerAccount2);
a.remove(customerAccount3);
 
sajjad ahmad
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI!
Thanks for reply yeah i was not properly commiting the transaction but i have solved that problem now and also thanks for the other tip as i was searching for this as well...it's realy very use full
 
sajjad ahmad
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Scott Johnson !
i am trying to do this according to your advice

I am trying to update a child records i have following scenario
master record (having one to many assciation with child table cascade=all-delete-orphan in .hbm.xml configuration file) ===> customerInfo
detail record set (having many to one assciation with master table) ===> customerAccountsSet

i do following steps to update a record
1) get the customerInfo object from session successfully
2) get the customerAccountsSet = customerInfo.getCustomerAccounts();
3) traying to add a new customerAccount say customerAccount1 to be saved for the first time without primery key in it(i am reating a new customerAccount object here)
4) traying to update a customerAccount say customerAccount2 to be updated with primery key in it(i have searched this object fron customerAccountSet on the basis of primery key)
5) traying to add a delete customerAccount say customerAccount3 with primery key in it(i have searched this object fron customerAccountSet on the basis of primery key)

only step 5 is not executed properly as i can still see the deleted record in db , although save and update steps have been successfully completed in db.
can anybody tell me whats going wrong here.code for steps 2,3,4,5 is as follows in the same order
2)Set customerAccountSet = (Set) customerInfo.getCustomerAccounts();
3)customerAccountSet.add(customerAccount1);
4)customerAccountSet.add(customerAccount2);
5)customerAccountSet.remove(customerAccount3);

thanks in advance.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic