java - Updating Multiple Rows - Hibernate -
in addresses table, have field named isprimary. each address assigned user, user can have 1 address primary.
i have method below trying fix. if new address created , has attribute isprimary set true, need update of users existing addresses , set isprimary false.
when method called , existing record has attribute set true. return null.
is possible this? use trigger, mysql not support triggers acting on table inserting/updating on.
this save method
@override public useraddress save(useraddress obj) throws userexception { if(obj.getisprimary()) { list<useraddress> addresslist = template.find("from useraddress userid = ?", obj.getuseraccount().getuserid()); for(iterator<useraddress> = addresslist.iterator(); i.hasnext();) { i.next().setisprimary(false); template.update(i.next()); } } template.saveorupdate(obj); return obj; }
the issue in code calling next() twice in loop. should work:
for(useraddress> addr: addresslist) { addr.setisprimary(false); template.update(addr); }
personnally, have made user class such as:
class user { private list<useraddress> addresses = new arraylist<useraddress>(); private useraddress primaryaddress; ... }
Comments
Post a Comment