hibernate - Managed entity is not getting persisted into the DB -
i have got below method :-
public staff authenticatestaff(loginbean loginbean) { staff = staffdao.findunique(loginbean.getusername()); if (null != staff) { if (staff.getisblocked() == 'n' && staff != null && staff.getusername().equals(loginbean.getusername()) && staff.getpassword().equals(loginbean.getpassword())) { staff.setlastlogin(new date()); new staffdaoimpl().update(staff); return staff; } else if (staff.getisblocked() == 'n' && staff != null && staff.getusername().equals(loginbean.getusername())) { updateunsuccessfulattemptsandblockedstatus(staff); } } return null; } in method see staff managed entity , calling below method 'updateunsuccessfulattemptsandblockedstatus' passing staff managed entity.
public void updateunsuccessfulattemptsandblockedstatus(staff staff) { int unsuccessfullloginattempts = staff.getunsuccessfullloginattempts(); staff.setunsuccessfullloginattempts(unsuccessfullloginattempts + 1); if (unsuccessfullloginattempts + 1 > noofallowedunsuccessfulattempts) staff.setisblocked('y'); new staffdaoimpl().update(staff); } persistence.xml
<?xml version = "1.0" encoding = "utf-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="expensecalculator" transaction-type="resource_local"> <properties> <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.driver" /> <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/expensecalc" /> <property name="javax.persistence.jdbc.user" value="dbauser" /> <property name="javax.persistence.jdbc.password" value="abcdefg" /> <property name="hibernate.dialect" value="org.hibernate.dialect.mysql5dialect" /> <property name="hibernate.show_sql" value="true" /> <!-- <property name="hibernate.hbm2ddl.auto" value="create" /> --> <!-- enabling second level cache --> <property name="hibernate.cache.use_second_level_cache" value="true" /> <property name="hibernate.cache.use_query_cache" value="true" /> <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.ehcacheregionfactory" /> <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.singletonehcacheregionfactory" /> </properties> </persistence-unit> </persistence> mydaoimpl class:-
public class genericdaoimpl<t> implements genericdao<t> { private static final string persistence_unit_name = "expensecalculator"; @persistencecontext(unitname = persistence_unit_name) private static entitymanagerfactory factory; protected static entitymanager em; protected entitytransaction etr; protected class<t> domainclass; /** domain object name. */ protected string domainobjectname = null; public synchronized static entitymanagerfactory getfactory(){ if (null == factory) { factory = persistence.createentitymanagerfactory(persistence_unit_name); } return factory; } @suppresswarnings("unchecked") public genericdaoimpl() { em = getfactory().createentitymanager(); domainclass = (class<t>) ((parameterizedtype) getclass().getgenericsuperclass()).getactualtypearguments()[0]; entity entityann = (entity) domainclass.getannotation(entity.class); if (entityann != null && !entityann.name().equals("")) { domainobjectname = entityann.name(); } else { domainobjectname = domainclass.getsimplename(); } } public t update(t t) { etr = em.gettransaction(); etr.begin(); etr.commit(); return t; } after users unsuccessful login method 'updateunsuccessfulattemptsandblockedstatus' called. value of attributes of staff table not getting persisted db staff entity not remaining managed.
can please explain why staff entity becoming unmanaged?
for managed entities hibernate automatically synchronizes entity state underlying database record. should handle entities inside of persistence context.
move authenticatestaff method genericdaoimpl , change this:
public staff authenticatestaff(loginbean loginbean) { etr = em.gettransaction(); etr.begin(); staff = staffdao.findunique(loginbean.getusername()); if (null != staff) { if (staff.getisblocked() == 'n' && staff != null && staff.getusername().equals(loginbean.getusername()) && staff.getpassword().equals(loginbean.getpassword())) { staff.setlastlogin(new date()); etr.commit(); return staff; } else if (staff.getisblocked() == 'n' && staff != null && staff.getusername().equals(loginbean.getusername())) { int unsuccessfullloginattempts = staff.getunsuccessfullloginattempts(); staff.setunsuccessfullloginattempts(unsuccessfullloginattempts + 1); if (unsuccessfullloginattempts + 1 > noofallowedunsuccessfulattempts) staff.setisblocked('y'); } } etr.commit(); return null; }
Comments
Post a Comment