Java 实体是否默认缓存在 jpa 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2707903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are entities cached in jpa by default?
提问by TCM
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.
我将实体添加到我的数据库中,它工作正常。但是当我检索列表时,我得到了旧实体,我添加的新实体在我取消部署应用程序并再次重新部署之前不会显示。这意味着我的实体是否默认缓存?但是,我没有在我的persistence.xml 或任何此类文件中对缓存实体进行任何设置。
I have even tried calling flush(), refresh() and merge(). But still it shows the old entities only. Am i missing something? Please help me.
我什至尝试过调用flush()、refresh() 和merge()。但它仍然只显示旧实体。我错过了什么吗?请帮我。
采纳答案by cletus
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.
欢迎来到 JPA。如果您使用它,则意味着如果您在 JPA 之外更新数据库,将会遇到很大的问题,除非您知道自己在做什么并且非常小心。这意味着您必须弄清楚如何刷新任何缓存的实体,以便它们可以重新加载。
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 之外的实体,如果您这样做,您可能必须进入特定 JPA 提供程序使用的缓存模型的工作原理。如果您需要在 JPA 之外进行大量更新,那么 JPA 可能不是您的正确选择。
回答by Pascal Thivent
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).
JPA 1.0 没有定义L2 缓存(“共享缓存”),JPA 1.0 只定义了一个L1 缓存(“事务缓存”),但 JPA 提供者可以支持共享对象缓存,而且大多数都支持。TopLink Essentials 就是这种情况,它通过JPA Extensions for Caching(每个 JVM)支持 L1 和 L2 缓存。
Now, as explained in the great article Understanding the cache of TopLink Essentials(GlassFish JPA):
现在,正如在了解 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).
- 来自同一持久性单元的所有 EntityManager 共享会话缓存(这就是 TopLink 调用第二级缓存的方式)。
- 会话缓存默认开启。
- 如果持久化上下文中的实体被修改/删除,它们会在事务提交后同步到会话缓存,因此会话缓存的状态会更新(或者这样的缓存根本不可用)。
So there must be something else wrong with your setup. You can trydo 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?
PS:这不能回答问题,但是,如果您使用的是 GlassFish v3,为什么不使用 EclipseLink?
Update: answering a comment of the OP
更新:回答 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 linkbetween 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
).
好吧,如果您不在Java 级别创建实体之间的链接,那么 JPA 将无法在数据库中创建它(JPA 只会执行您告诉他的操作)。所以,是的,你需要创建的链接,并在双向关联的情况下,你甚至需要设置链路的两端(例如添加employee
到员工对收集Department
和设置department
的Employee
)。
回答by memozac
Are you using the EntityManager? if not give it a try http://docs.oracle.com/javaee/5/api/javax/persistence/EntityManager.html
你在使用 EntityManager 吗?如果不试试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.
您是否使用数据源来管理与数据库的连接?你也应该试试。是在您的服务器中配置的 xml。提供有关您的架构的更多信息,以便我们为您提供帮助。
回答by James
JPA 2.0 defines a shared (L2) cache, but does not specify the default. EclipseLink enables caching by default, other providers do not.
JPA 2.0 定义了共享 (L2) 缓存,但未指定默认值。EclipseLink 默认启用缓存,其他提供程序则不启用。
An EntityManager will always have a persistence context (L1) cache until you call clear() or create a new one.
EntityManager 将始终具有持久性上下文 (L1) 缓存,直到您调用 clear() 或创建一个新缓存为止。
You can disable the shared cache,
您可以禁用共享缓存,
See, http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching
见,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.
但是你的问题是你没有维护双方关系。设置 1-1 时需要添加到 1-m,否则您的对象无效。
For more information on caching see,
有关缓存的更多信息,请参见
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching
回答by godtree
I am using the context of eclipselinkand 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
:
我正在使用eclipselink和ejb的上下文,我发现当我查询实体时,它会被自动缓存,但我也运行了一个函数来更改数据库中的记录,所以我可能会在缓存中获取旧数据,所以我禁用缓存通过将以下内容添加到文件中persistence.xml
:
<shared-cache-mode>NONE</shared-cache-mode>
it do works!
它确实有效!