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

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

I thought we should specify all classes associated with a particular persistence unit. But I accidentally forgot to specify the element class for the new object, but the program worked perfectly even without it. Then I deleted all the elements of the class, and everything worked fine. So why do we need this?

code example:

 <persistence-unit name="JiraManager" transaction-type="RESOURCE_LOCAL"> <class>chartdemo.model.domain.Category</class> </persistence-unit> 
+6
java spring jpa persistence
source share
2 answers

If you did not specify classes in the persistence.xml file, the persistence.xml manager will manage all entity classes in the location where the persistence.xml file exists (jar file, class directory).

Listing classes provide the flexibility to select objects and group them in a storage unit. You can have finer control, consisting of a given unit constant name - you can also include entites from other banks, etc.

Thus, in most cases, class listing is not necessary, and most entities will find all entities for you.

+7
source share

Some persistence providers (such as Hibernate) scan the class path for entity classes.

+3
source share

All Articles