hibernate - Detached object reattached with cascade=CascadeType.MERGE + @Version = Staleobjectexception -
i have understood marking method cascade=cascadetype.merge lead detached entities reattached may involve roundtrip database
public class pollvote{ @manytoone(cascade=cascadetype.merge,fetch=fetchtype.lazy,optional=true) @joincolumn(name="fk_userid",nullable=true,updatable=false) public user getuser() { return user; }
in case, user class has @version property
public class user{ @column @version @jsonignore public date getlastupdate() { return lastupdate; }
however, in instances, i'm getting staleobjectstateexception. realize must occurring because user object i'm passing take session , may therefore stale. in case largely irrelevant because needed id of user object save pollvote.
my first question is: on cascade=cascadetype.merge: if pass in stale object @version attribute, shouldn't hibernate refresh object database in stead of throw staleobjectstateexception?
my second question is: why staleobjectstateexception thrown when needed id, immutable?
you're merging pollvote , have configured hibernate cascade merge user of pollvote. hibernate checks state of user it's merging has not been modified other transaction in meantime. that's whole point of optimitic locking.
it seems don't want merge state of user when merging pollvote. in case, remove cascade=merge attribute, , use entitymanager.getreference()
reference user want pollvote attached to.
Comments
Post a Comment