Hibernate does not create a new entry in table -
i have question. possible create new entry in db if entry not exist? if exist should update current entry.
i have this:
@transactional public void savesettingsforuser(string userid, settingsmessagemodel settings) { session session = getsessionfactory().getcurrentsession(); usersettings usersettings = new usersettings(); usersettings = (usersettings) session.load(usersettings.class, userid); usersettings.setuserid(userid); usersettings.setautoanswer(settings.isautoanswer()); usersettings.setdnd(settings.isdnd()); session.save(usersettings); }
it works entries in db. if want save new 1 primery key userid not allow me. suggestion appreciated. thank you!
i managed create new entry if such id not exist following code:
@transactional public void savesettingsforuser(string userid, settingsmessagemodel settings) { session session = getsessionfactory().getcurrentsession(); usersettings usersettings = (usersettings) session.get( usersettings.class, userid); if (usersettings == null) { usersettings = new usersettings(); } usersettings.setuserid(userid); usersettings.setautoanswer(settings.isautoanswer()); usersettings.setdnd(settings.isdnd()); session.saveorupdate(usersettings); }
instead of session.load() use session.get() method. approach allows me check if there entry in db , if there not create it. hope else.
Comments
Post a Comment