views:

144

answers:

2

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>
+2  A: 

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

Bozho
Does it mean that in general case we should not add class-element inside persistence-unit?
Roman
yes (15 chars req)
Bozho
+3  A: 

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.

cetnar
I found that we can prevent managing all entity classes only using <exclude-unlisted-classes> element.
Roman