2017-08-28 95 views
0

我有一個java代碼,我正在更新員工實體。我正在使用應用程序管理實體管理器。我在登錄頁面有一個條件,如果用戶提供了正確的用戶名/密碼,他的最後登錄時間將被更新。管理實體越來越分離

但更新不會發生,我得到一個異常的實體已經detached.Please看到下面的代碼: -

GenericDaoImpl.java

public class GenericDaoImpl<T> implements GenericDao<T> { 
    private static final String PERSISTENCE_UNIT_NAME = "expenseCalculator"; 
    private static EntityManagerFactory factory; 
    protected static EntityManager em; 
    private 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 create(T t) { 
     etr = em.getTransaction(); 
     etr.begin(); 
     em.persist(t); 
     etr.commit(); 
     return t; 
    } 
} 

StaffServiceImpl.java

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()); 
//This is giving an exception.. 
        new StaffDaoImpl().create(staff); 
        return staff; 
       } else if (staff.getIsBlocked() == 'N' && staff != null 
         && staff.getUserName().equals(loginBean.getUserName())) { 
        updateUnsuccessfulAttemptsAndBlockedStatus(staff); 
       } 
      } 
      return null; 
     } 

StaffDaoImpl.java: -

@Repository 
@Component 
public class StaffDaoImpl extends GenericDaoImpl<Staff> implements StaffDao { 
    public Staff findUnique(String userName) { 
     TypedQuery<Staff> query = em.createNamedQuery(domainObjectName + ".findByUserName", domainClass); 
     query.setParameter("username", userName); 
     Staff staff = null; 
     try { 
      staff = query.getSingleResult(); 
     } catch (NoResultException e) { 
      staff = null; 
     } 
     return staff; 
    } 

在Class StaffServiceImpl.java中,您將看到我已經讓人員實體管理您調用了「staffDao.findUnique(loginBean.getUserName());」但是它仍然讓我體會到實體職員是獨立的。任何幫助,高度讚賞。

+0

你得到什麼異常究竟 –

+0

org.hibernate.PersistentObjectException:?脫離實體被persist:com.expensecalculator.domain.Staff \t在org.hibernate.event.internal.DefaultPersistEventListener.onPersist( DefaultPersistEventListener.java:124) \t at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58) \t在org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:778) \t在org.hibernate.internal.SessionImpl.persist(SessionImpl.java:751) \t在org.hibernate.internal.SessionImpl.persist( SessionImpl.java:756) –

+0

由於這是一個更新,你可以考慮使用'merge'而不是'persist' – Pelocho

回答

0

我已經得到了答案,實際上「」staffDao.findUnique(loginBean.getUserName())「並不會讓人員實體進行託管,所以我必須使用合併,以便人員實體進行管理並對其進行任何更改被永久保存到數據庫