为什么我们需要在< persistence-unit>中指定类?元件? [英] Why do we need to specify class inside <persistence-unit> element?
问题描述
我的 persistence.xml
有2个持久性单位。他们每个人都有几个< class>
元素。
My persistence.xml
has 2 persistence-units. Each of them has several <class>
elements.
我认为我们必须指定与某些相关的所有类持久性的单元。但我不小心忘了为新实体指定class-element,但程序工作正常,即使没有它。然后我删除了所有类元素,一切正常。那么,为什么我们需要呢?
I thought that we must specify all classes related to certain persistence-unit. But I accidentally forgot to specify class-element for new entity but the program worked fine even without it. Then I removed all class-elements and everything worked fine. So, why do we need it?
示例代码:
<persistence-unit name="JiraManager" transaction-type="RESOURCE_LOCAL">
<class>chartdemo.model.domain.Category</class>
</persistence-unit>
推荐答案
如果你没有在<$中指定类c $ c> persistence.xml 文件您的持久性管理器将管理persistence.xml文件所在位置的所有实体类(jar文件,类目录)。
If you didn't specify classes in the persistence.xml
file your persistence manager will manage all entity classes in location where persistence.xml file exists (jar file, class directory).
列表类使您可以灵活地选择实体并将它们分组为持久性单元。您可以更精确地控制包含给定持久性单位名称的内容 - 您还可以包含来自其他罐子的entites等。
Listing classes gives you flexibility to choose entities and group them in persistence unit. You can have finer control what consist a given persistent unit name - you can also include entites from other jars etc.
因此,在大多数基本情况下,列出类是不必要的,并且大部分JPA强制措施都会为您发现所有实体。
So, in most basic cases listing classes is unnecessary, and most of JPA impementations discover all entities for you.
这篇关于为什么我们需要在< persistence-unit>中指定类?元件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!