我在显示数据库中的数据时遇到问题。如果我更新一个对象,有时我会得到旧数据,有时会得到新数据。更新功能运行良好(我可以在数据库中看到正确的更新),而读取功能似乎获取缓存数据。我尝试禁用两个缓存,尝试在更新/保存期间打开和关闭会话,但它仍然无法正常工作。User 和 Store bean 都有 Lazy fetches。谢谢!
阅读功能:
public static List<Store> getStoreByUser(User user)
throws HibernateException {
List<Store> result = null;
Session session = sessionFactory.getCurrentSession();
Transaction transaction = null;
try {
transaction = session.getTransaction();
Criteria criteria = session.createCriteria(Store.class);
criteria.add(Restrictions.eq("userUserID", user));
result = criteria.list();
} catch (HibernateException he) {
logger.error("No Store found for user: = " + user, he);
throw he;
}
return result;
}
更新/保存功能:
public static Integer insertOrUpdateStore(Store store)
throws HibernateException {
Integer id = null;
Session session = sessionFactory.getCurrentSession();
Transaction transaction = null;
try {
transaction = session.getTransaction();
if (store.getStoreID() != null && store.getStoreID() != 0) {
session.merge(store);
transaction.commit();
} else {
id = (Integer) session.save(store);
transaction.commit();
}
} catch (HibernateException he) {
if (transaction != null) {
transaction.rollback();
}
} finally {
}
return id;
}