I'm unable to get my query to show updated results, I've tried various options and solutions to other questions on this and other forums but nothing as yet works.
The only way I can get the updated results is by relaunching my app.
Notes:
- I'm running each query in a new session (and have confirmed the session is new).
- I'm editing the data externally so there's no transaction to commit (and confirmed the data is updated).
The Hibernate properties I've tried (from various answers to other questions):
<property name="hibernate.cache.use_second_level_cache">false</property>
<property name="hibernate.cache.use_query_cache">false</property>
<property name="hibernate.connection.isolation">1</property>
The session factory code (non-relevant code removed for brevity):
public static SessionFactory createSessionFactory() {
final Configuration configuration = new Configuration();
// removed code to read database properties (host, port, user, password etc)
configuration.addProperties(properties);
// removed code to add entities
configuration.addAnnotatedClass(... various entities ...);
// the particular entity that I'm querying
configuration.addAnnotatedClass(User.class);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
The open session code:
public static Session openSession() {
getDatabasePreferences();
if (sessionFactory == null) {
sessionFactory = createSessionFactory();
}
return sessionFactory.openSession();
}
The query code:
public static List getList(Class entity, String order, Boolean reverseOrder, Boolean enabled) throws HibernateException {
Session session = null;
List list = null;
try {
session = openSession();
// below causes exception (java.lang.IllegalArgumentException: Non-entity object instance passed to evict : class my.entity.User)
session.evict(entity);
// makes no difference
session.clear();
// removed code that determines sort order, other filters, etc...
list = session.createCriteria(entity)
// below statements (from other answers) have no effect
.setCacheable(false)
.setCacheMode(CacheMode.REFRESH)
.setFlushMode(FlushMode.AUTO)
} catch (HibernateException e) {
throw new HibernateException(e);
} finally {
try {
if (session != null) {
session.close();
}
} catch (HibernateException e) {
e.printStackTrace();
}
}
if (list != null) {
if (list.isEmpty()) {
logger.log(Level.INFO, "List " + entity.getSimpleName() + " is empty.");
} else {
//logger.log(Level.INFO, "Obtained " + entity.getSimpleName() + " List: " + list);
logger.log(Level.INFO, "Obtained " + entity.getSimpleName());
}
} else {
logger.log(Level.WARNING, "Unable to obtain " + entity.getSimpleName() + " List.");
}
return list;
}