简体   繁体   中英

Managed Entity is getting detached

I have a java code where i am updating the staff entity. I am using the application managed entity manager. i have a condition in the login page where if the user has given correct username/password his last login time will be updated.

But the update is not happening and i am getting an exception that the entity has been detached.Please see the Code below :-

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

In the Class StaffServiceImpl.java you will see that i have made the Staff entity managed y calling the "staffDao.findUnique(loginBean.getUserName());" but still it gives me exception that the entity Staff is detached. Any help is highly appreciated.

我得到了答案,实际上““ staffDao.findUnique(loginBean.getUserName())”不会使Staff实体受管理。因此,我必须使用merge来管理Staff实体,并将对它的任何更改都保存到数据库中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM