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

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


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.