java - Hibernate Get Detached Object in Service, alter the ID and persist again -
i have asked this question here , same. im looking different approach.
i have java spring mvc application. trying crud on 1 table, employee.
i trying object database using hibernate, changes including altering id, , save again. doing in service layer
i have following code in serviceimpl
public class employeeserviceimpl extends genericmanagerimpl<employee, long> implements employeeservice { @override public void saveold(long key) { employee = (employee) employeedao.get(key); employee.setactive(false); employee.setempkey(null); employeedao.save(employee); } }
but getting exception.
org.springframework.orm.hibernate4.hibernatesystemexception: identifier of instance of com.table.model.employee altered 4 null; nested exception org.hibernate.hibernateexception: identifier of instance of com.table.model.employee altered 4 null
from the question told above, have option use copy constructor, copy values except key , in service set other values , save. , working.
employee = (employee) employeedao.get(key); employee = new employee(employee); employee.setactive(false); employeedao.save(employee);
i looking different apporach. how can object detached 1 session, alter values including id , persist again? should in service layer.
thanks.
you can commons beanutils
org.apache.commons.beanutils.propertyutils.copyproperties( dest, source );
do below
employee source = (employee) employeedao.get(key); employee dest= new employee(); org.apache.commons.beanutils.propertyutils.copyproperties( dest, source ); dest.setid(null); dest.setactive(false); employeedao.save(dest);
Comments
Post a Comment