简体   繁体   English

休眠getResultList()返回不同的数据

[英]hibernate getResultList() returns varying data

I am working with JPA (1.0 or 2.) + hibernate (3.4.0 or 3.6.0) and I have run into a problem with I think caching somewhere. 我正在使用JPA(1.0或2)+休眠(3.4.0或3.6.0),并且在某些地方缓存时遇到了问题。 What I do: 我所做的:

  1. Find an object with my JPA class (row in the database with a particular id) 用我的JPA类查找对象(在数据库中具有特定ID的行)
  2. update a boolean flag on the object (tinyint field in the database) 更新对象上的布尔标志(数据库中的tinyint字段)
  3. persist the object 坚持对象
  4. grab the entire table from the database with getResultList() hoping to have the change reflected. 使用getResultList()从数据库中获取整个表,希望能够反映出更改。

Problem: 问题:

The change is reflected with getResultList the first time I call it, but the second time it show the previous state. 第一次调用该更改时,它会在getResultList中反映出来,但是第二次显示前一个状态。 The third time it shows correctly; 第三次正确显示; the fourth, the previous state; 第四,以前的状态; etc. It seems to alternate between the two state each time I call getResultList on the table. 等等。每次我在表上调用getResultList时,似乎都在两种状态之间交替。

Some code for #3 above: 上面#3的一些代码:

EntityTransaction entityTransaction = entityManager.getTransaction();
entityTransaction.begin();
entityManager.persist(object);
entityTransaction.commit();
entityManager.refresh(object);

Code for #4: #4的代码:

Query query = entityManager.createQuery("from " + object.getName());
List<T> resultList = query.getResultList();

In my efforts to solve the problem, I have: 在解决问题的努力中,我有:

1.Turned L2 and query cache off in the persistence.xml with: 1.使用以下命令在persistence.xml中关闭L2和查询缓存:

<property name="hibernate.cache.use_query_cache" value="false"/>
<property name="hibernate.cache.use_second_level_cache" value="false"/>

2.forced a cache eviction before running getResultList() with (using JPA 2.0): 2.在使用(使用JPA 2.0)运行getResultList()之前强制驱逐缓存:

entityManager.getEntityManagerFactory().getCache().evictAll()

3.tried calling refresh() all over the place - to no effect. 3.尝试在各处调用refresh()-无效。

Am I missing something? 我想念什么吗?

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks, sop 谢谢你

I have encountered this issue before (or something very similar) with a few different JPA providers. 我在与几个不同的JPA提供程序之前(或类似情况)遇到过此问题。 Make sure that you are explicitly closing your EntityManagers. 确保您明确关闭了EntityManager。

I don't think entityManager.refresh(object); 我不认为entityManager.refresh(object); is necessary. 有必要的。 The default flush mode is Auto, so EntityManager will flush (synchronize the persistence context to the underlying database) at query execution. 默认刷新模式为自动,因此EntityManager将在查询执行时刷新(将持久性上下文同步到基础数据库)。

Furthermore, does each query entail a SQL query from the database? 此外,每个查询是否都需要从数据库进行SQL查询?

Well, Steven, actually your solution did finally end up helping. 好吧,史蒂文,实际上您的解决方案最终得到了帮助。 In my application, I call my updating methods from a Servlet. 在我的应用程序中,我从Servlet调用更新方法。 The problem went away as soon as I closed the EntityManager before I called any of my methods. 一旦我在调用任何方法之前关闭EntityManager,问题就消失了。 I still don't know why this works. 我仍然不知道为什么这样。

Upon closer inspection of the problem, I found that it wasn't getResultList() that was the problem, but when I did a find() on the object in question after updating it, that find() would alternate between two objects with different hashes but same IDs (the different hash was a result of the boolean flag being set or not.) 仔细检查问题,我发现不是问题是getResultList(),但是当我在更新后对相关对象执行find()时,find()会在两个具有不同对象的对象之间交替哈希但ID相同(不同的哈希是是否设置了布尔标志的结果。)

This is still really weird, but Steven's suggestion worked once I found the right place to do it. 这仍然很奇怪,但是一旦我找到合适的位置,史蒂文的建议就起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM