开发者

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

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_LOCA开发者_StackOverflowL">
    <class>chartdemo.model.domain.Category</class>
</persistence-unit>


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜