0

Can't test a jpa/maven project. I have the error "javax.persistence.PersistenceException: No Persistence provider for EntityManager named xxx" when I run "mvn cleant test" from commandline. I have a Java SE project.

I have the persistence confiuration in 'src/test/resources/META-INF/persistence.xml'. And I also has the same for 'src/main/...'. I can see the persistence.xml in 'target/classes/META-INF'. Only that is from the main, not from the test as I run the tests. This is not yet the problem, since both should work anyway. Trying the JPA for the first time, but as I see, the files should be ok (location and content).

The persistence unit names should match also.

I'm using Eclipse (EE) with m2 and other necessary plugins, but running maven from commandline. I see no errors in the project.

// Update

Tried fixing the maven build as I noticed it should have the test classes and resources in 'target/test-classes'. Changed the command to "mvn clean test-compile test" Now the resources can be found from the correct place, but I still got the same error.

// update

For clarity here's the full persistence.xml

<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="xxx"
    transaction-type="RESOURCE_LOCAL">
    <provider>my.package.EntityManagerFactoryHelper</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/db" />
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.user" value="yyy" />
        <property name="javax.persistence.jdbc.password" value="zzz" />
        <property name="eclipselink.ddl-generation" value="DROP_AND_CREATE" />
    </properties>
</persistence-unit>

I took the helper example from other posts. Basically it just creates the emf using the 'xxx' persistence unit. Here's the helper class. http://pastebin.com/1GE6uMa1

Ville Myrskyneva
  • 1,560
  • 3
  • 20
  • 35
  • See [this related](http://stackoverflow.com/questions/1158159/no-persistence-provider-for-entitymanager-named) SO question – Raghuram May 22 '12 at 06:39

2 Answers2

0

Try to add < provider>org.hibernate.ejb.HibernatePersistence< /provider> inside tag

Nurlan
  • 505
  • 1
  • 4
  • 11
  • Tried. The error was: "javax.persistence.PersistenceException: Invalid persistence.xml." – Ville Myrskyneva May 22 '12 at 07:42
  • Resolved this problem partly. Looks like the solution was to use the above provider and make fixes with the maven pom. – Ville Myrskyneva May 22 '12 at 09:11
  • Seems I had the maven-pom too complicated. Also seems like I have misunderstood something with the helper class using it as provider. I still use the helper, but not as provider. Here's the pom for buiding the project, incase somebody else ends up here: http://pastebin.com/SvRKqAYs – Ville Myrskyneva May 22 '12 at 09:23
0

The provider has to be:

     <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

Else the table creation fails, even that there is no errors when creating the tables from the entities.

Spent about 2 days resolving this, since I thought I was pointed the right direction by the given hint and didn't check the provider then.

The provider has to be according to the eclipselink, as you could spot it in the configuration of the 'persistence.xml'.

But I failed to say it explicitly. Didn't know it would matter that much and it even succeeded with the table creation once. Not really sure what I had for the provider then, but it failed subsequently (after changes made to the table). Probably there should had been other configurations by the provider.

Ville Myrskyneva
  • 1,560
  • 3
  • 20
  • 35