I have very simple persistance.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="eventractor" transaction-type="RESOURCE_LOCAL">
<class>pl.michalmech.eventractor.domain.User</class>
<class>pl.michalmech.eventractor.domain.Address</class>
<class>pl.michalmech.eventractor.domain.City</class>
<class>pl.michalmech.eventractor.domain.Country</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
and it works.
But when I remove <class>
elements application doesn't see entities (all classes are annotated with @Entity
).
Is there any automatic mechanism to scan for @Entity
classes?
Hibernate doesn't support
<exclude-unlisted-classes>false</exclude-unlisted-classes>
under SE, (another poster mentioned this works with TopLink and EclipseLink).There are tools that will auto-generate the list of classes to persistence.xml e.g. the Import Database Schema wizard in IntelliJ. Once you've got your project's initial classes in persistence.xml it should be simple to add/remove single classes by hand as your project progresses.
for JPA 2+ this does the trick
scan all jars in war for annotated @Entity classes
No, you don't necessarily. Here is how you do it in Eclipse (Kepler tested):
Right click on the project, click Properties, select JPA, in the Persistence class management tick Discover annotated classes automatically.
In Java SE environment, by specification you have to specify all classes as you have done:
and
(JSR-000220 6.2.1.6)
In Java EE environments, you do not have to do this as the provider scans for annotations for you.
Unofficially, you can try to set
<exclude-unlisted-classes>false</exclude-unlisted-classes>
in your persistence.xml. This parameter defaults tofalse
in EE andtrue
in SE. Both EclipseLink and Toplink supports this as far I can tell. But you should not rely on it working in SE, according to spec, as stated above.You can TRY the following (may or may not work in SE-environments):
Not sure if you're doing something similar to what I am doing, but Im generating a load of source java from an XSD using JAXB in a seperate component using Maven. Lets say this artifact is called "base-model"
I wanted to import this artifact containing the java source and run hibernate over all classes in my "base-model" artifact jar and not specify each explicitly. Im adding "base-model" as a dependency for my hibernate component but the trouble is the tag in persistence.xml only allows you to specify absolute paths.
The way I got round it is to copy my "base-model" jar dependency explictly to my target dir and also strip the version of it. So whereas if I build my "base-model" artifact it generate "base-model-1.0-SNAPSHOT.jar", the copy-resources step copies it as "base-model.jar".
So in your pom for the hibernate component:
Then I call the hibernate plugin in the next phase "process-classes":
and finally in my persistence.xml I can explicitly set the location of the jar thus:
and add the property:
It's not a solution but a hint for those using Spring:
I tried to use
org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
with settingpersistenceXmlLocation
but with this I had to provide the<class>
elements (even if thepersistenceXmlLocation
just pointed toMETA-INF/persistence.xml
).When not using
persistenceXmlLocation
I could omit these<class>
elements.