托管实体不会持久存入数据库

时间:2017-08-15 13:20:31

标签: hibernate jpa

我有以下方法: -

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;
    }

在这种方法中,您将看到工作人员是一个托管实体,我正在调用以下方法'updateUnsuccessfulAttemptsAndBlockedStatus'传递人员管理实体。

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课程: -

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;
    /** The 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;
    }

用户登录失败后,调用方法'updateUnsuccessfulAttemptsAndBlockedStatus'。但是Staff表的属性值不会持久保存到DB中,因为Staff实体不会保持管理状态。

任何人都可以解释为什么员工实体不受管理?

1 个答案:

答案 0 :(得分:0)

对于托管实体,Hibernate会自动将实体状态与底层数据库记录同步。但是你应该处理持久化上下文中的实体。

authenticateStaff方法移至GenericDaoImpl并将其更改为:

    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;
    }