我将实体添加到数据库中并且工作正常。但是当我检索列表时,我得到旧实体,我添加的新实体不会显示,直到我取消部署应用程序并再次重新部署它。这意味着我的实体默认被缓存吗?但是,我没有在 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.
发布评论
评论(5)
欢迎来到 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.
JPA 1.0 没有定义L2 缓存(“共享缓存”),JPA 1.0 仅定义L1 缓存(“事务缓存”),但 JPA 提供程序可以支持共享对象缓存,大多数都是这样。这是 TopLink Essentials 的情况,它通过 用于缓存的 JPA 扩展(每个 JVM)。
现在,正如这篇精彩文章 了解 TopLink Essentials 的缓存( GlassFish JPA):
所以你的设置肯定还有其他问题。您可以通过添加以下属性尝试出于测试目的(并且仅出于测试目的)禁用共享会话缓存:
但如果这会改变任何内容,我会感到惊讶。正如我所说,我认为某个地方还存在另一个问题。
PS:这并不能回答问题,但是,如果您使用的是 GlassFish v3,为什么不使用 EclipseLink?
更新:回答OP的评论
好吧,如果您不在 Java 级别的实体之间创建链接,JPA 将无法在数据库中创建它(JPA 只会执行您告诉他执行的操作)。因此,是的,您需要创建链接,并且在双向关联的情况下,您甚至需要设置链接的两侧(例如将
employee
添加到Department
上的员工集合,并设置Employee
的department
)。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):
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:
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
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 theDepartment
and set thedepartment
of anEmployee
).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
您使用实体管理器吗?如果没有尝试一下 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.
我正在使用 eclipselink 和 ejb 的上下文,我发现当我查询实体时,它会自动缓存,但我还运行一个函数来更改数据库中的记录,所以我可能会在缓存中获取旧数据,因此我通过将以下内容添加到文件
persistence.xml
来禁用缓存:它确实有效!
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
:it do works!