当外部插入记录时,Hibernate集合不会更新 [英] Hibernate collection not updated when records inserted externally

查看:111
本文介绍了当外部插入记录时,Hibernate集合不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个映射集合,当我在另一个应用程序中向外部添加项目时,它不会更新。



禁用二级缓存。



示例...

  session = HibernateDataSource.openSession(); 
User dao =(User)session.load(User.class,2434152);
//现在没有项目,这会给出0
System.err.println(dao.getItems()。size());
session.close();
Thread.sleep(10000);
//在外面添加一个项目,例如在PMA
session = HibernateDataSource.openSession();
HibernateDataSource.getSessionFactory()。evict(User.class);
HibernateDataSource.getSessionFactory()。evict(UserItem.class);
HibernateDataSource.getSessionFactory()。evictCollection(User.class.getName()+.items);
dao =(User)session.load(User.class,2434152);
//仍为零
System.err.println(dao.getItems()。size());
session.close();

在Google搜寻时,我尝试了几种解决方案。将集合设置为脏。没有工作。



有没有其他的东西我没有看过?



首先尝试Hibernate 3.2.7。升级到3.3.2后,没有什么区别。

解决方案

95%的时间花在Hibernate上后,它已经被免除了。

这个问题是由于MySQL的查询缓存造成的,尽管存在这个名称,但它也缓存了可重复读取的结果集。这是由于MySQL InnoDB的多版本化的特性造成的。



为了设置快照的新时间点,将您的工作单元包装在交易,即使只有选择查询。



进一步阅读...



http://forums.mysql.com/read.php?39416790,416790



http ://dev.mysql.com/doc/refman/5.0/en/query-cache.html


I've a mapped collection that is not updated when I add items to that collection externally, in another application.

Second-level caching is disabled.

Example...

session = HibernateDataSource.openSession();
User dao = (User) session.load(User.class, 2434152);
// No items now, this gives 0
System.err.println(dao.getItems().size());
session.close();
Thread.sleep(10000);
// Add an item outside, e.g. in PMA
session = HibernateDataSource.openSession();
HibernateDataSource.getSessionFactory().evict(User.class);
HibernateDataSource.getSessionFactory().evict(UserItem.class);
HibernateDataSource.getSessionFactory().evictCollection(User.class.getName() + ".items");
dao = (User) session.load(User.class, 2434152);
// Still zero
System.err.println(dao.getItems().size());
session.close();

I've tried several solutions when googling, e.g. setting the collection as dirty. None worked.

Is there something else that I haven't looked at?

P.S. First tried on Hibernate 3.2.7. Upgraded to 3.3.2, no difference.

解决方案

After spending 95% of the time hacking into Hibernate, it has been exonerated.

The issue arose due to MySQL's query caching, which, despite the name, also caches resultsets for repeatable reads. This is due to the nature of multi-versioning on MySQL's InnoDB.

In order to "set a new time point for a snapshot", wrap your unit of work in a transaction, even if there are only select queries.

Further reading...

http://forums.mysql.com/read.php?39,416790,416790

http://dev.mysql.com/doc/refman/5.0/en/query-cache.html

这篇关于当外部插入记录时,Hibernate集合不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆