HIbernate 查询问题(可能与缓存相关)
HIbernate Query Issue (Possibly Cache Related)
我无法让我的查询显示更新的结果,我已经尝试了各种选项和解决方案来解决这个论坛和其他论坛上的其他问题,但目前还没有任何效果。
获得更新结果的唯一方法是重新启动我的应用程序。
备注:
- 我 运行 在新会话中进行每个查询(并确认会话是新的)。
- 我正在外部编辑数据,因此没有要提交的事务(并确认数据已更新)。
我尝试过的 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;
}
顺便说一句,我过去遇到过这个问题并解决了它刷新会话。如果这不是您的问题,请查看此 post,也许可以帮助您:
Hibernate reading function shows old data
我的问题中指定的 Hibernate 属性未应用于会话。在我的会话工厂方法中手动添加属性后,我的查询现在 returns 更新了数据...
现在包括:
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");
我无法让我的查询显示更新的结果,我已经尝试了各种选项和解决方案来解决这个论坛和其他论坛上的其他问题,但目前还没有任何效果。
获得更新结果的唯一方法是重新启动我的应用程序。
备注:
- 我 运行 在新会话中进行每个查询(并确认会话是新的)。
- 我正在外部编辑数据,因此没有要提交的事务(并确认数据已更新)。
我尝试过的 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;
}
顺便说一句,我过去遇到过这个问题并解决了它刷新会话。如果这不是您的问题,请查看此 post,也许可以帮助您: Hibernate reading function shows old data
我的问题中指定的 Hibernate 属性未应用于会话。在我的会话工厂方法中手动添加属性后,我的查询现在 returns 更新了数据...
现在包括:
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");