默认情况下,实体缓存在 jpa 中吗?

发布于 2024-08-30 04:15:18 字数 200 浏览 11 评论 0 原文

我将实体添加到数据库中并且工作正常。但是当我检索列表时,我得到旧实体,我添加的新实体不会显示,直到我取消部署应用程序并再次重新部署它。这意味着我的实体默认被缓存吗?但是,我没有在 persistence.xml 或任何此类文件中对缓存实体进行任何设置。

我什至尝试过调用flush()、refresh() 和merge()。但它仍然只显示旧实体。我错过了什么吗?请帮我。

I add entity to my database and it works fine. But when i retrieve the List, i get the old entity, the new entities i add are not shown until i undeploy the application and redeploy it again. This means are my entities cached by default? But, I haven't made any settings for caching entities in my persistence.xml or any such file.

I have even tried calling flush(), refresh() and merge(). But still it shows the old entities only. Am i missing something? Please help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浅笑依然 2024-09-06 04:15:18

欢迎来到 JPA。如果您使用它,则意味着如果您在 JPA 之外更新数据库,将会遇到巨大的问题,除非您知道自己在做什么并且非常小心。这意味着您必须弄清楚如何刷新任何缓存的实体,以便可以重新加载它们。

基本上,如果您可以帮助的话,请不要更新 JPA 之外的实体,如果您这样做,您可能必须了解特定 JPA 提供程序使用的缓存模型的工作原理。如果您需要在 JPA 之外进行大量更新,那么 JPA 可能不是您的正确选择。

Welcome to JPA. If you use it, it means you will have huge problems if you update the database outside of JPA unless you know what you're doing and are very careful. This means you have to figure out how to flush any cached entities so they can be reloaded.

Basically, don't update entities outside JPA if you can at all help it and if you do you will probably have to get into the workings of the caching model used by your particular JPA provider. If you need to update outside JPA a lot then JPA probably isn't the right choice for you.

尛丟丟 2024-09-06 04:15:18

这意味着我的实体默认被缓存吗?

JPA 1.0 没有定义L2 缓存(“共享缓存”),JPA 1.0 仅定义L1 缓存(“事务缓存”),但 JPA 提供程序可以支持共享对象缓存,大多数都是这样。这是 TopLink Essentials 的情况,它通过 用于缓存的 JPA 扩展(每个 JVM)。

现在,正如这篇精彩文章 了解 TopLink Essentials 的缓存( GlassFish JPA)

  • 来自同一持久单元的所有 EntityManager 共享会话缓存(这就是 TopLink 调用二级缓存的方式)。
  • 会话缓存默认打开。
  • 如果持久化上下文中的实体发生修改/删除,它们会在事务提交后同步到会话缓存,从而更新会话缓存的状态< /strong> (或者这样的缓存根本无法使用)。

所以你的设置肯定还有其他问题。您可以通过添加以下属性尝试出于测试目的(并且仅出于测试目的)禁用共享会话缓存:

<property name="toplink.cache.shared.default" value="false"/>

但如果这会改变任何内容,我会感到惊讶。正如我所说,我认为某个地方还存在另一个问题。

PS:这并不能回答问题,但是,如果您使用的是 GlassFish v3,为什么不使用 EclipseLink?

更新:回答OP的评论

因此,如果我保留员工记录,那么它会在数据库中看到,但不会在部门员工集合中看到,直到我将其显式添加到员工集合中。这是必要的步骤吗?

好吧,如果您不在 Java 级别的实体之间创建链接,JPA 将无法在数据库中创建它(JPA 只会执行您告诉他执行的操作)。因此,是的,您需要创建链接,并且在双向关联的情况下,您甚至需要设置链接的两侧(例如将employee 添加到Department 上的员工集合,并设置 Employeedepartment)。

This means are my entities cached by default?

JPA 1.0 does not define a L2 cache ("shared cache"), JPA 1.0 only defines an L1 cache ("transactional cache") but JPA providers can support a shared object cache, and most do. This is the case of TopLink Essentials which supports L1 and L2 cache through JPA Extensions for Caching (per JVM).

Now, as explained in the great article Understanding the cache of TopLink Essentials(GlassFish JPA):

  • All EntityManagers from same persistence unit shares the session cache (that's how TopLink calls the 2nd-level cache).
  • Session cache is turned on by default.
  • If there is modifications/deletions of entities in the persistence context they are synchronized to session cache after a transaction committed, so the state of session cache is updated (or such a cache wouldn't be usable at all).

So there must be something else wrong with your setup. You can try do disable the shared session cache for testing purpose (and only for testing purpose) by adding the following property:

<property name="toplink.cache.shared.default" value="false"/>

But I would be surprised if this changes anything. As I said, I think there is another problem somewhere.

PS: This doesn't answer the question but, if you are using GlassFish v3, why don't you use EclipseLink?

Update: answering a comment of the OP

So if i persist employee record, then it is seen in database but not in collection of employees in department until i explicitly add it to the collection of employees. Is this necessary step?

Well, if you don't create the link between entities at the Java level, JPA won't be able to create it at the database (JPA only does what you tell him to do). So, yes, you need to create the link and, in case of a bidirectional association, you even need to set both sides of the link (for example add the employee to the collection of employees on the Department and set the department of an Employee).

凉薄对峙 2024-09-06 04:15:18

JPA 2.0 定义了共享(L2)缓存,但未指定默认值。 EclipseLink 默认启用缓存,而其他提供程序则不启用缓存。

EntityManager 将始终具有持久性上下文 (L1) 缓存,直到您调用clear() 或创建新的缓存。

您可以禁用共享缓存,

请参阅 http://wiki.eclipse.org/ EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching

但你的问题是你没有维护双方的关系。当你设置1-1时,你需要添加到1-m,否则你的对象是无效的。

有关缓存的更多信息,请参阅

http://wiki.eclipse.org/EclipseLink/用户指南/JPA/Basic_JPA_Development/缓存

JPA 2.0 defines a shared (L2) cache, but does not specify the default. EclipseLink enables caching by default, other providers do not.

An EntityManager will always have a persistence context (L1) cache until you call clear() or create a new one.

You can disable the shared cache,

See, http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching

But your problem is that you are not maintain both sides of your relationship. When you set the 1-1 you need to add to the 1-m, otherwise your objects are invalid.

For more information on caching see,

http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching

翻了热茶 2024-09-06 04:15:18

您使用实体管理器吗?如果没有尝试一下 http://docs.oracle.com /javaee/5/api/javax/persistence/EntityManager.html

您是否使用数据源来管理与数据库的连接?你也应该尝试一下。是在您的服务器中配置的 xml。提供有关您的架构的更多信息,以便我们为您提供帮助。

Are you using the EntityManager? if not give it a try http://docs.oracle.com/javaee/5/api/javax/persistence/EntityManager.html

Are you using a DataSource to manage connections to your database? you should try it also. Is an xml that is configured in your server. Give more information about your architecture so we can help you.

陌生 2024-09-06 04:15:18

我正在使用 eclipselinkejb 的上下文,我发现当我查询实体时,它会自动缓存,但我还运行一个函数来更改数据库中的记录,所以我可能会在缓存中获取旧数据,因此我通过将以下内容添加到文件 persistence.xml 来禁用缓存:

<shared-cache-mode>NONE</shared-cache-mode>

它确实有效!

I am using the context of eclipselink and ejb,i found that when I query entities,it will auto be cached,but I also run a function to change record in database,so I may get old data in the cache,so I disable cache by adding the following to the file persistence.xml:

<shared-cache-mode>NONE</shared-cache-mode>

it do works!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文