Hibernate + JPA查询中过时的数据

时间:2016-07-12 20:00:26

标签: hibernate jpa

我注意到我的Spring应用程序存在问题,我正在实现内部消息传递服务并创建显示未知消息的数量,每当新消息到达时,每次更新都会实现值的变化。 简而言之,每当插入或更新发生时,每次新咨询都会更新过时的更新。

例如:当我显示未读消息的数量时,结果会随着每次更新而变化如下:1,2,1,2,1 ...... 每个系统都会出现同样的问题,但其他功能不太明显。

我已经尝试禁用Hibernate的缓存并清楚地说明了EntityManager,但问题仍然存在。

通用类

public class GenericDAOImpl<T> implements IGenericDAO<T> {

private static EntityManagerFactory emf;
private static final Logger logger = Logger.getLogger(GenericDAOImpl.class);

public GenericDAOImpl() {
    if (emf == null) { 
        emf = Persistence.createEntityManagerFactory("contratos");
    }
}

//@Override
public <T> Object findById(int id, Class<T> clazz) throws SQLException {
    EntityManager em = emf.createEntityManager();
    T instance = null;
    try {
        instance = em.find(clazz, id);
    } catch (RuntimeException re) {
        logger.error(re.getMessage(), re);
        throw re;
    }

    return instance;
}

@Override
public void add(T obj) throws SQLException {

    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    try {
        //if (!em.contains(obj)) { 
            em.persist(obj);
        //}
        em.getTransaction().commit();
    } catch (Exception e) {
        em.getTransaction().rollback();
        logger.error(e.getMessage(), e);
    } finally {
        em.close();         
    }
}

@Override
public <T> List<T> listAll(Class<T> clazz) throws SQLException {
    EntityManager em = emf.createEntityManager();
    TypedQuery<T> qry = em.createQuery("select a from " + clazz.getSimpleName() + " a", clazz);
    List<T> objs = qry.getResultList();

    em.close();
    return objs;
} 

@Override
public <T> List<T> busca(Class<T> clazz, String campo, String busca) throws SQLException {
    EntityManager em = emf.createEntityManager();

    TypedQuery<T> qry = em.createQuery("select a from " + clazz.getSimpleName() + " a where " + campo + " = '" + busca + "'", clazz);
    List<T> objs = qry.getResultList();

    em.close();
    return objs;
} 

@Override
public void remove(T obj, int id,  Class<T> clazz) throws SQLException {
    EntityManager em = emf.createEntityManager();

    T obj2 = em.getReference(clazz, id);
    em.getTransaction().begin();
    try {

        em.remove(obj2);

        em.getTransaction().commit();
        em.flush();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        em.getTransaction().rollback();
    } finally {
        em.close();         
    }
}

@Override
public void edit(T obj) throws SQLException {

    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    //em.find(obj.getClass(), obj);
    try {
        //if (em.contains(obj)) { 
            em.merge(obj);
        //}
        em.getTransaction().commit();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        em.getTransaction().rollback();
    } finally {
        em.close();         
    }
}

@Override
public boolean contains(T obj) throws SQLException {
    EntityManager em = emf.createEntityManager();
    try {
        return em.contains(obj); 
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        return false;
    } finally {
        em.close();         
    }
}
}

0 个答案:

没有答案