no persistence provider for EntityManager named persistence.xml

不想你离开。 提交于 2019-12-01 14:16:15
lexicore

I just can't resist, this is my final attempt to help. :) I'm writing this for you and for any users who may encounter similar issues.

I believe you simply have a problem in the Eclipse project configuration. The paths in your Eclipse project do not seem correct.

Normally, when you import an existing Maven project into Eclipse using File > Import... > Existing Maven Projects, m2eclipse should analyse the structure of the Maven project and create Eclipse project with (.classpath and .project files) automatically.

The paths in these projects are derived from your Eclipse pom.xml. So you actually should not create/modify/add/remove/copy/move/whatever Eclipse source folders. If you do this, your Eclipse project will no longer be synchronized with your Maven project and you'll get really weird results.

Sometimes m2eclipse does not do its job well. Sometimes you don't get the right source folders, things go missing etc. From my experience, this got radically better over the time and right now m2eclipse works like a charm. From what I saw in your struggles, it might be that you have an older version of m2eclipse. Maybe not, but I'd suggest to check it anyway.

Next, sometimes m2eclispe still clamps. In this cases it is highly recommended to do the following things:

  • Clean the project: Project > Clean...
  • Update the Maven project (Alt + F5). Make sure to update the project configuration from pom.xml

Finally you should get the following project structure:

Specifically, you should get the following source folders:

  • src\main\java - source folder for your main Java files
  • src\main\resources - source folder for resources
  • target\generated-sources\xjc - source folder for the generated code.
    • Important: not target\generated-sources like you have but target\generated-sources\xjc.
    • This folder contains the generated Java classes. (Hyperjaxb adds JPA annotations to them).
    • It also contains META-INF\persistence.xml which is a persistence unit descriptor. The META-INF\persistence.xml resource is specified by the JPA spec. This is where the persistence unit will be loaded from.
    • If you've generated a roundtrip test (xml-to-the-db-and-back), then you'll also see a RoundtripTest class somewhere. This is a JUnit test class, it should be possible to execute it directly. (Run As > JUnit Test)
  • src\test\java - test classes.
  • src\test\resources - test resources

This is a standard directory layout for Maven projects. The only peculiarity is the target\generated-sources\xjc source folder which holds the code generated by the schema compiler. And even this is absolutely standard, it is a usual convention for Maven code-generation plugins to generate code in target\generated-sources\some-tool directories.

Below I post the .classpath file from my project:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" output="target/classes" path="src/main/java">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry including="**/*.java" kind="src" output="target/classes" path="target/generated-sources/xjc">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="src" output="target/test-classes" path="src/test/java">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="output" path="target/classes"/>
</classpath>

Important

If you don't have this directory structure (and your screenshots show that you don't) DO NOT move on. It will fail (it does for you). Fix your project structure first. Do not move on, don't try to run anything, you'll run into all kinds of strange things like not found resources etc. (which you do).

Regenerate project structure from pom.xml and clean the project in Eclipse. Open your .classpath file and compare it line-by-line with what I posted. But please do fix the project structure first.

Next, it is important that you understand that you are not supposed to move or drag-and-drop the generated code. It is not the right thing to do. Don't do it. Please don't move on until you accept this as an imperative. Don't move or modify the generated code. If you do, you overtake the responsibility for this code. Things are generated the way they are because it works this way. So if you change it it is very likely that it won't work anymore (it does not for you). If you will need to change anything, you change the pom.xml which configures the generation, not the resulting code.

Now, let us analyse your current project structure based on the following screenshot:

What I can notice:

  • src\main\java contains maintest\Main and TestFunctions. If you're trying to write a test, put your test classes into src\test\java, not src\main\java.
  • src\main\resources contains persistence.xml, sun-jaxb.episode, persistence.properties and schema.xsd. Only schema.xsd should be there.
    • persistence.xml and sun-jaxb.episode are generated files. And, see above, you must not move them.
    • persistence.properties defines the connection to the database used for testing. So it must be in test resources, namely src\test\resources. Putting it into src\main\resources will finally add this file into the resulting JAR, it's like telling everyone username and password of the database you use for testing
  • src\test\java - this is OK, but your test classes should be here.
  • src\test\resources - persistence.properties should be here.
  • target\generated-sources - this is wrong, this should be target\generated-sources\xjc
    • org.jvnet.hyperjaxb3.ejb.test.po package is under target\generated-sources. It is generated under target\generated-sources\xjc, so what I see means you've moved. Don't do this, see above.
    • xjc is displayed as package. This is actually a leftover after you've (incorrectly) configured target\generated-sources as a source folder. This folder must contain all of the generated sources, now it only contains the RoundtripTest. Which means you moved the generated code. Don't do this, see the imperative above.

So your Eclipse project configuration as displayed above is invalid. Here's my theory on why this happened. I think that your m2eclipse did not process the pom.xml correctly, you were probably missing folders etc. But instead of getting the Eclipse project files to be correctly generated my m2eclipse you have manually added the target\generated-sources and started moving the generated code around. This is not the right way.

Please check and re-check your .classpath until it is equivalent to what I posted. I have zipped and uploaded my sample project which definitely works for me. You can unzip and import it using File > Import... > Existing Projects into Eclipse (not as Maven project).

Finally, you must be able to execute the RoundtripTest as a JUnit test. The test will probably fail, but it must run, you should not be getting NPEs etc. I don't have a MySQL database at hand, so this is what I get at the moment:

Detected [file:/C:/Projects/workspaces/hj3/hyperjaxb3-ejb-template-basic-0.6.0/target/classes/META-INF/persistence.xml].
    RoundtripTest

Loading entity manager factory properties.
    RoundtripTest

Loading entity manager factory properties from [file:/C:/Projects/workspaces/hj3/hyperjaxb3-ejb-template-basic-0.6.0/target/test-classes/persistence.properties].
    RoundtripTest

Could not obtain connection to query metadata
    org.hibernate.cfg.SettingsFactory

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.net.ConnectException
MESSAGE: Connection refused: connect

This means that entity manager started correctly but could not connect to the database. If you've configured your test database in the src\test\resources\persistence.properties corretcly, you should be able to connect to the database. And if you'll put a po.xml file to src\test\samples, the roundtrip test will:

  • unmarshal this files
  • save it into the database
  • load it back
  • compare it to the original result

Please get the roundrip test run first before you start your own experiments. Don't copy the code, don't modify it, don't try to bend it to your directory structure. It must run as it is. Don't move on until it does.

Good luck.

Update.

You might also be facing an issue with unresolved dependencies. Please make sure your dependencies are resolved correctly. This is what I see:

If you don't see this, it is bad. Something is wrong with your workspace. Check if you have any error messages in Eclipse. Maybe you have something like:

Plugin execution not covered by lifecycle configuration: org.codehaus.mojo:hibernate3-maven-plugin

Quick-fix it first ("Permanently mark goal ... as ignored in Eclispe build"). Don't move on until this error is gone. This is a blocker.

The parameter you should pass is the persistence-unit. (Not the file name.)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence version="1.0" ... >
    <persistence-unit name="generated">
        <class>generated.Items</class>
        <class>generated.Items$Item</class>
        <class>generated.PurchaseOrderType</class>
        <class>generated.USAddress</class>
    </persistence-unit>
</persistence>

So it should be generated for the persistence.xml above:

entityManagerFactory = Persistence.createEntityManagerFactory("generated", persistenceProperties);

For your persistence, should be:

entityManagerFactory = Persistence.createEntityManagerFactory("org.jvnet.hyperjaxb3.ejb.tests.po", persistenceProperties);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!