Location of JPA persistence.xml to auto find entities

I’m working on some example code to show different configuration options and approaches for using Hibernate with JPA. I just noticed that the location of the /META-INF/persistence.xml file is critical to allow Hibernate to auto-locate your annotated entities.

In order for the auto location to work (to avoid having to explicitly list annotated entities in your persistence.xml or hibernate.cfg.xml files), the persistence.xml file must be bundled in the same jar as the entities. If you move the file elsewhere, then the entities are not found, even if they are in the classpath. To workaround this, if you do need to put the persistence.xml file in a different location, use the <mapping> element to explicitly declare the entities.

One Reply to “Location of JPA persistence.xml to auto find entities”

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.