0

I have a Result table. I am fetching a result row using result_id and then using result details.

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.

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 ?

1 Answer 1

1

The Session.refresh() method can be used to fetch the latest state from the database:

session.refresh(instance)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.