java - Prevent previously loaded entity being updated in transactional method -
i have following method in 1 of services
@override @transactional public void createuserinfo(long userid, userinfo userinfo) { user olduser = userservice.finduserbyid(userid); if (olduser.infoisthesame(userinfo)) { return; } user updateduser = userservice.adduserinfo(userid, userinfo); feedservice.addfeed(feedaction.info_update, olduser, updateduser); }
as can see finduserbyid()
, adduserinfo()
methods in same service, marked @transactional
itself.
as userservice.finduserbyid()
executes in same transaction createuserinfo()
started, olduser
instance affected change of user info inside adduserinfo()
(as loaded managed entity in same transaction).
so problem when trying addfeed()
olduser
, updateduser
same after service method calls.
so question if there way change olduser
not affected?
note changing adduserinfo()
start new transaction not option me, used in several other places.
i tried copy olduser
(e.g. via beanutils) issue stays.
thnx help.
use entitymanager.detach()
, detach entity persistence context:
user olduser = userservice.finduserbyid(userid); if (olduser.infoisthesame(userinfo)) { return; } em.detach(olduser); user updateduser = userservice.adduserinfo(userid, userinfo); feedservice.addfeed(feedaction.info_update, olduser, updateduser);
Comments
Post a Comment