where does Persistence.createEntityManagerFactory() look for the persistence unit?

17,371

It searches your persistence.xml, located inside your META-INF dir. This folder has to be at the same level as your root package.

In a maven project, the folder would be in /src/main/resources/META-INF/persistence.xml

Share:
17,371

Related videos on Youtube

CodeMed
Author by

CodeMed

Updated on September 15, 2022

Comments

  • CodeMed
    CodeMed over 1 year

    I am using eclipse. I am trying to debug a failure to find the persistence unit that is indeed located in the directory structure of my eclipse project.

    Where does EntityManagerFactory javax.persistence.Persistence.createEntityManagerFactory(String persistenceUnitName, Map properties) look for the persistence unit?

    When I ask where it looks, I mean all the possible resources that could get searched, including eclipse configurations, properties in persistence.xml if the file is found, resources searched by included JARs, etc. To explore how this works, I am executing a test program by right clicking on a Main.java class located in the root (src/main/java) of an eclipse project.

    The answer might help me figure out how to configure the eclipse project so that EntityManagerFactory javax.persistence.Persistence.createEntityManagerFactory(String persistenceUnitName, Map properties) can find the persistence unit.

  • mendieta
    mendieta over 9 years
    Ok, your META-INF folder has to be inside src/main/resources
  • mendieta
    mendieta over 9 years
    How are you executing your example? If you are running from a maven test package, then maybe you have to declare the persistence in src/test/resources.. But to be honest, I'm not so sure if it is needed.
  • CodeMed
    CodeMed over 9 years
    +1 for your help so far, even though I am looking for a deeper answer that can help people who google search this to really understand what is going on inside the inner plumbing.
  • mendieta
    mendieta over 9 years
    hyperjaxb creates a persistence.xml and places it in your target folder.. maybe this is interfering with the one placed in the resources folder.. maybe you can try removing the persistence from the output dir, and run your example again
  • CodeMed
    CodeMed over 9 years
    My issue was created by moving around things that were created by a combination of hyperjaxb and the pom.xml file that built the hyperjaxb project. I resolved my issue by making changes to the project build before it got into eclipse. I am going to mark yours as the answer to thank you for the time you put into this.