我无法得到我的查询以显示更新的结果,我已经在此论坛和其他论坛上尝试了其他问题的各种选项和解决方案,但尚无效。
我可以通过重新启动我的应用来获得更新结果的唯一方法。
注意:
我尝试过的Hibernate属性(从其他问题的各种答案):
<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>
会话工厂代码(为简洁起见,删除了不相关的代码):
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;
}
开放会话代码:
public static Session openSession() {
getDatabasePreferences();
if (sessionFactory == null) {
sessionFactory = createSessionFactory();
}
return sessionFactory.openSession();
}
查询代码:
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;
}
答案 0 :(得分:0)
我在过去的btw中遇到了这个问题,并解决了冲洗会话的问题。如果不是你的问题看看这篇文章,也许可以帮助你: Hibernate reading function shows old data
答案 1 :(得分:0)
我的问题中指定的Hibernate属性未应用于会话。我的查询现在在我的会话工厂方法中手动添加属性后返回更新的数据...
现在包括:
configuration.setProperty("hibernate.c3p0.max_statements","0");
configuration.setProperty("hibernate.cache.use_second_level_cache","false");
configuration.setProperty("hibernate.cache.use_query_cache","false");
configuration.setProperty("hibernate.connection.isolation","1");