Hibernate如何查询最新数据而不是缓存中的数据
我创建了一个登录函数,该函数将使用Hibernate查询从mysql检索数据。但是,这个登录函数总是从缓存中检索数据,而不是直接从数据库中检索数据,即使在我通过另一个应用程序将数据修改到同一个数据库中之后也是如此 下面是我的代码:Hibernate如何查询最新数据而不是缓存中的数据,hibernate,Hibernate,我创建了一个登录函数,该函数将使用Hibernate查询从mysql检索数据。但是,这个登录函数总是从缓存中检索数据,而不是直接从数据库中检索数据,即使在我通过另一个应用程序将数据修改到同一个数据库中之后也是如此 下面是我的代码: public User login(User entity) throws Exception { Transaction trns = null; Session session = null; User data = null; t
public User login(User entity) throws Exception {
Transaction trns = null;
Session session = null;
User data = null;
try {
session = SessionUtil.getSessionFactory().openSession();
trns = session.beginTransaction();
DaoInterface dao = new UserDaoImpl();
data = dao.findByUniqueKey(session, entity);
} catch (Exception e) {
if (trns != null) {
trns.rollback();
}
throw e;
} finally {
session.flush();
session.close();
}
return data;
}
我试图在findByUniqueKey函数中的session.close()和session.execute之前添加session.clear(),但仍然无法检索最新数据
我正在寻求有关如何解决此问题的建议。请尝试使用Session.about()。放弃删除会话中存储的所有对象。我刚刚通过在data=dao.findByUniqueKey(会话,实体)之后添加trns.commit()修复了我的问题;在登录方法中。添加代码后,该方法将始终检索下一次调用的最新数据。 我不明白为什么查询需要提交函数。
有人可以提供建议吗?您好,我无法从hibernate会话或会话API获取Session.about()函数。
public User findByUniqueKey(Session session, User entity)
throws HibernateException {
User data = null;
try {
Query query = session
.createQuery("FROM User where loginUserId = :loginUserId ");
query.setCacheMode(CacheMode.REFRESH);
query.setString("loginUserId", entity.getLoginUserId());
data = (User) query.uniqueResult();
} catch (HibernateException e) {
throw e;
}
return data;
}