在 JPA (eclipselink) 中禁用缓存 [英] Disable caching in JPA (eclipselink)

查看:51
本文介绍了在 JPA (eclipselink) 中禁用缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 JPA (eclipselink) 从我的数据库中获取数据.数据库已被许多其他来源更改,因此我想为我执行的每个查找返回到数据库.我已经阅读了许多关于禁用缓存的帖子,但这似乎不起作用.有什么想法吗?

I want to use JPA (eclipselink) to get data from my database. The database is changed by a number of other sources and I therefore want to go back to the database for every find I execute. I have read a number of posts on disabling the cache but this does not seem to be working. Any ideas?

我正在尝试执行以下代码:

I am trying to execute the following code:

        EntityManagerFactory entityManagerFactory =  Persistence.createEntityManagerFactory("default");
        EntityManager em = entityManagerFactory.createEntityManager();

        MyLocation one = em.createNamedQuery("MyLocation.findMyLoc").getResultList().get(0);

        MyLocation two = em.createNamedQuery("MyLocation.findMyLoc").getResultList().get(0);    

        System.out.println(one==two);

one==two 是真的,而我希望它是假的.

one==two is true while I want it to be false.

我尝试将以下每个/所有内容添加到我的persistence.xml

I have tried adding each/all the following to my persistence.xml

<property name="eclipselink.cache.shared.default" value="false"/>
<property name="eclipselink.cache.size.default" value="0"/>
<property name="eclipselink.cache.type.default" value="None"/>

我还尝试将 @Cache 注释添加到实体本身:

I have also tried adding the @Cache annotation to the Entity itself:

@Cache(
  type=CacheType.NONE, // Cache nothing
  expiry=0,
  alwaysRefresh=true
)

我是不是误解了什么?

推荐答案

这个行为是正确的,否则如果你用不同的值改变对象一和对象二,你会在持久化它们时出现问题.发生的事情是对加载对象的调用更新了在第一次调用中加载的实体.它们必须指向同一个对象,因为它们是同一个对象.这样可以确保不能写入脏数据.

This behavior is correct, otherwise if you change object one and object two with different values you will have problems when persisting them. What is happening is the call to load object two updates the entity loaded in the first call. They must point to the same object since they ARE the same object. This ensures that dirty data cannot be written.

如果您在两次调用之间调用 em.clear(),则实体 1 应该分离,您的支票将返回 false.然而,没有必要这样做,eclipse 链接实际上是将您的数据更新到最新的,我猜这就是您想要的,因为它经常更改.

If you call em.clear() between the two calls, entity one should become detached your check will return false. There is however no need to do that, eclipse link is infact updating your data to the latest which I would guess is what you want since it frequently changes.

附带说明,如果您希望使用 JPA 更新此数据,您需要获取 悲观锁 实体,使底层数据无法在数据库中更改.

On a side note if you wish to update this data using JPA you will need to be obtaining pessimistic locks on the Entity so that the underlying data cannot change in the DB.

您将需要禁用查询缓存以及您的缓存选项只是从播放中删除对象缓存而不是查询缓存,这就是您没有获得新结果的原因:

You will need to disable the query cache as well your cache options were just removing the object cache from play not the query cache, that is why you are not getting the new results:

在您的代码中:

em.createNamedQuery("MyLocation.findMyLoc").setHint(QueryHints.CACHE_USAGE, CacheUsage.DoNotCheckCache).getResultList().get(0);

或者在persistence.xml中:

Or in persistence.xml:

<property name="eclipselink.query-results-cache" value="false"/>

这篇关于在 JPA (eclipselink) 中禁用缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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