[英]How to get updated values from db table in the same hibernate session
I have a Result table. 我有一个结果表。 I am fetching a result row using result_id and then using result details. 我正在使用result_id获取结果行,然后使用结果详细信息。
I am updating some of the values of result table: 我正在更新结果表的一些值:
try {
Result instance = (Result) getTransactionSession().get("com.xxx.Result", id); //$NON-NLS-1$
commit();
if (instance == null) {
ResultHome.log.debug("get successful, no instance found"); //$NON-NLS-1$
} else {
updateResultStepsSequence(id, instance);
ResultHome.log.debug("get successful, instance found"); //$NON-NLS-1$
}
return instance;
} catch (RuntimeException re) {
ResultHome.log.error("get failed", re); //$NON-NLS-1$
rollback();
throw re;
}
And 和
private void updateResultStepsSequence(long resultId, Result resultInstance) {
if (resultInstance.getStepCount() > 0) {
List<ResultStep> resultSteps = resultInstance.getResultSteps();
if (resultSteps != null && !resultSteps.isEmpty() && (resultSteps.get(0).getDurationSum() == 0)) {
try {
Session session = SessionFactoryProvider.getSessionFactory().getCurrentSession();
Query query = session.getNamedQuery("updateResultStepSequence").setLong("result_id", //$NON-NLS-1$ //$NON-NLS-2$
resultId);
query.executeUpdate();
commit();
} catch (RuntimeException re) {
ResultHome.log.error("Query execution failed", re); //$NON-NLS-1$
rollback();
throw re;
}
}
}
}
After executing the updateResultStepsSequence() method, I can see that the table is getting updated, but the instance object is still holding the old values prior to the DB update. 执行updateResultStepsSequence()方法后,我可以看到表正在更新,但是实例对象在数据库更新之前仍保持旧值。
How can I get the updated values from the table? 如何从表中获取更新的值? I tried creating a new session, updating the table and closing the newly created session. 我尝试创建一个新会话,更新表并关闭新创建的会话。
With this approach I am able to get the latest updated values from the table. 通过这种方法,我可以从表中获取最新的更新值。
Is there a better way of doing this ? 有更好的方法吗?
The Session.refresh() method can be used to fetch the latest state from the database: Session.refresh()方法可用于从数据库中获取最新状态:
session.refresh(instance)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.