I have an issue where an object that was updated outside of Hibernate (using MySQL Workbench) is not getting refreshed. What happens is that I do a call to the Hibernate application and check the value, then do an update to a value of the object using Workbench, save and check that the value is set and do another call in Hibernate after refreshing and still the old value is there. The object does gets resfreshed on the third call but I need this to happen straight away — I cannot afford to use old data in this instance.
Query q = session.createQuery("SELECT object(o) FROM PortedNumber o WHERE o.number = '" + agiRequest.getDnid() + "'");
List<PortedNumber> portedNumberList = q.list();
if (!portedNumberList.isEmpty()) {
for (PortedNumber portedNumber : portedNumberList) {
session.evict(portedNumber);
session.refresh(portedNumber, LockOptions.READ);
networkId = portedNumber.getNetwork().getId();
logger.info("Number is ported to networkId = " + networkId);
break;
}
}
The value that I update is the ‘portedNumber.getNetwork().getId()’.
Any guidance would be most weclome
Thanks.
In Hibernate 3, we have merge() (in Hibernate 2, use saveOrUpdateCopy()). This method will force Hibernate to copy any changes from other detached instances onto the instance you want to save, and thus merges all the changes in memory before the save.