Why do we need to specify class inside <persistence-unit> element?

守給你的承諾、 提交于 2019-12-07 02:24:02

问题


My persistence.xml has 2 persistence-units. Each of them has several <class> elements.

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?

sample code:

<persistence-unit name="JiraManager" transaction-type="RESOURCE_LOCAL">
    <class>chartdemo.model.domain.Category</class>
</persistence-unit>

回答1:


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).

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.

So, in most basic cases listing classes is unnecessary, and most of JPA impementations discover all entities for you.




回答2:


Some persistence providers (Hibernate for example) scan the classpath for entity classes.



来源:https://stackoverflow.com/questions/1999307/why-do-we-need-to-specify-class-inside-persistence-unit-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!