简体   繁体   中英

Maven can't find persistence.xml

I was trying to do a simple guice persistence test, unfortunately I got this:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named test
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32)
at com.google.inject.persist.jpa.JpaPersistService.start(JpaPersistService.java:94)
at utfpr.edu.br.client.PersistenceTestModule.getInjector(PersistenceTestModule.java:22)
at utfpr.edu.br.client.PessoaDaoTeste.<init>(PessoaDaoTeste.java:19)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:187)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:236)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:233)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:76)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

First : My persistence.xml is in the correct place resources/META-INF Second : Yes, I put the correct provider : org.hibernate.ejb.HibernatePersistence

Can someone help?

Your persistence.xml should be found, during runtime, at

WEB-INF/classes/META-INF/ .

However, putting into development folder resources/META-INF/ is the right thing to do, but not sufficient. You have to ensure that resources folder is used as "source folder" in eclipse.

To use a folder as source, right-click on the folder at package explorer panel, then context-menu -> build path -> use as source folder.

Then during eclipse WTP build, Java source files would be compiled and their bytecode class files placed into WEB-INF/classes folder.

Meanwhile, non-Java resources found in the source build path would be copied wholesale into WEB-INF/classes folder - and that is where your JPA driver should find the persistence defn file.

Fix'd I don't know why but I modified my pom.xml changing all guice version's by:

<version>${guice.version}</version>

also the properties

<properties>
    <guice.version>3.0</guice.version>
</properties>

and it work's, thanks anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM