Using Spring’s PersistenceXMLLocation in Persistence.xml

My question:

Is there a way to make Spring/JPA automatically detect classes annotated with @Entity?

Background:

This is my configuration of entityManagerFactory





……This is my persistence.xml …

 
org.hibernate.ejb.HibernatePersistence



This configuration is normal. But: when I add the following line to my entityManagerFactory configuration

JPA did not find the class annotated with @Entity . So, I get an exception like this:

java.lang.IllegalArgumentException: Unknown entity: foo.Bar

where foo.Bar is a @javax.persistence.Entity annotated class

When I add now

foo.Bar

To my persistence.xml, everything is fine. But why do I have to specify my class in persistence.xml when I use persistenceXmlLocation, otherwise it won’t work?

Note: You may ask me why I should use persistenceXmlLocation: it will solve this problem.

You didn’t say exactly what it was, so maybe I’m wrong, but it looks like you think I’m in a trap. The location of the persistence.xml file defines where Spring (or any JPA provider) is by default Find the @Entity class. From the JPA spec, section 8.2.1

A persistence.xml file defines a persistence unit. The persistence. xml file is located in the
META-INF directory of the root of the persistence unit.
It may be used to specify managed persistence
classes included in the persistence unit, object/relational mapping information for those classes, and
other configuration information for the persistence unit and for the entity manager(s) and entity manager factory for the persistence unit. This information may be defined by containment or by reference,
as described below.

The object/relational mapping information can take the form of annotations on the managed persistence
classes included in the persistence unit
, an orm.xml file contained in the META-INF directory of the
root of the persistence unit, one or more XML files on the classpath and referenced from the persistence.xml file, or a combination of these.
The managed persistence classes may either be contained within the root of the persistence unit; or they
may be specified by reference—ie, by naming the classes, class archives, or XML mapping files< br> (which in turn reference classes) that are accessible on the application classpath; or they may be specified by some combination of these means.

Is persistenceXmlLocation, but it should probably be called persistenceXmlName, because the purpose is to be able to create a persistence.xml file with a non-standard name so that the EE container will not pick it up. It can still be used as a tag for finding entity classes.

< /div>

My question:

Is there a way to make Spring/JPA automatically detect classes annotated with @Entity?

Background:

This is my configuration of entityManagerFactory





……This is my persistence.xml …

 
org.hibernate.ejb.HibernatePersistence



This configuration is normal. But: when I add the following line to my entityManagerFactory configuration

JPA did not find the class annotated with @Entity . So, I get an exception like this:

java.lang.IllegalArgumentException: Unknown entity: foo.Bar

where foo.Bar is a @javax.persistence.Entity annotated class

When I add now

foo.Bar

To my persistence.xml, everything is fine. But why do I have to specify my class in persistence.xml when I use persistenceXmlLocation, otherwise it won’t work?

Note: You may ask me why I want to use persistenceXmlLocation: it will solve this problem.

You did not say exactly what it is What, so maybe I’m wrong, but it looks like you think I’m in a trap. The location of the persistence.xml file defines where Spring (or any JPA provider) looks for @Entity classes by default. From the JPA spec, section 8.2. Section 1

A persistence.xml file defines a persistence unit. The persistence.xml file is located in the
META-INF directory of the root of the persistence unit.
It may be used to specify managed persistence
classes included in the persistence unit, object/relational mapping information for those classes, and
other configuration information for the persistence unit and for the entity manager(s) and entity manager factory for the persistence unit. This information may be defined by containment or by reference,
as described below.

The object/relational mapping information can take the form of annotations on the managed persistence
classes included in the persistence unit
, an orm.xml file contained in the META-INF directory of the
root of the persistence unit, one or more XML files on the classpath and referenced from the persistence.xml file, or a combination of these.
The managed persistence classes may either be contained within the root of the persistence unit; or they
may be specified by reference—ie, by naming the classes, class archives, or XML mapping files
(which in turn reference classes) that are accessible on the application classpath; or they may be specified by some combination of these means.

So although the field is called persistenceXmlLocation, it should probably be called persistenceXmlName because the purpose is to be able to create A persistence.xml file with a non-standard name so that the EE container will not pick it up. It can still be used as a tag for finding entity classes.

Leave a Comment

Your email address will not be published.