With Hibernate you can load your Entity classes as:
sessionFactory = new AnnotationConfiguration()
.addPackage("test.animals")
.addAnnotatedClass(Flight.class)
.addAnnotatedClass(Sky.class)
.addAnnotatedClass(Person.class)
.addAnnotatedClass(Dog.class);
Is there a way to do the same thing – programmatically loading your Entity classes – in a JPA 2.0 compliant way?
The reason for this question is because I’d like to dynamically load my Entity classes, thus not necessarily programmatically.
With the help of Spring I did this in a JPA compliant way.
My “persistence.xml” looks empty, with no entities listed within the
<persistence-unit>element.I then wrote a class that implemented PersistenceUnitPostProcessor like so:
Then I adjusted my spring context xml like this:
Note the registration of the
ReflectionsPersistenceUnitPostProcessorin the persistenceUnitPostProcessors setting.And that’s it. Every class with a JPA Entity or
MappedSuperclassannotation on the classpath is added to the classpath. I had to give reflections the prefix of a package name to scan through which is whycom.austinmichaelis there at all. You could register a secondReflectionsPersistenceUnitPostProcessorwith a different package name prefix if you want if your entities don’t share a common package name prefix.But, this is now JPAVendor agnostic.