views:

335

answers:

3

im developing a standalone application and it works fine when starting it from my ide(intellij idea), but after creating an uberjar and start the application from it javax.persistence.spi.PersistenceProvider is thrown saying "No Persistence provider for EntityManager named testPU"

here is my persistence.xml which is placed under meta-inf directory:

 <persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL">
     <provider>org.hibernate.ejb.HibernatePersistence</provider>
     <class>test.model.Configuration</class>
     <properties>
         <property name="hibernate.connection.username" value="root"/>
         <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
         <property name="hibernate.connection.password" value="root"/>
         <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/>
         <property name="hibernate.show_sql" value="true"/>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
         <property name="hibernate.c3p0.timeout" value="300"/>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
     </properties>
 </persistence-unit>

and here is how im creating the entity manager factory:

emf = Persistence.createEntityManagerFactory("testPU");

im using maven and tried the assembly plug-in with the default configuration fot it, i dont have much experience with assembling jars and i dont know if im missing something, so if u have any ideas ill be glad to hear them

A: 

I remember having that issue, and I think the problem was that you can't reference a jar in a jar. For your external libraries, they need to be expanded inside your jar or live elsewhere on the system CLASSPATH. So since you don't specify what all you're putting in your jar, I'm betting on this one.

You can NOT use the -cp command line parameter if you run your jar using the -jar parameter. One or the other.

TomR
not sure if it got clear, through the maven's assembly plug-in im creating an uberjar, this plugin by default expands all the dependency jars inside my jar so everything i need should be in my jar and it seems to be as i take a look in it ... further more ive tried to create the entity manager factory when giving a map of properties the result was the same, which confuses me even more i thought its something with the paths
alabalaa
Ah, I apologize. I missed "uberjar" as a specific term, as opposed to a generic one. Have you tried jarring it, then unjarring it on another box and running that way? I recall having this issue, and judging by the "Related" sidebar to our right, many other have as well, but I can't recall my specific fix, though I suspect it was "un-jar and run it not-in-a-jar". Sorry for misunderstanding the "uberjar" reference.
TomR
A: 

When using the Maven Assembly Plugin with the predefined jar-with-dependencies descriptor, you get a jar archive which contains the binary output of your project, along its the unpacked dependencies. So one possible problem I can think of would be multiple JARs with persistence.xml, in which case I'm not sure which one you'll get in the final assembly.

Since you get an error message complaining about "No Persistence provider for EntityManager named testPU", I would open the megajar and:

  • check that the persistence.xml is present (it should)
  • check that it contains the expected persistence unit testPU
    • if it doesn't, find the origin of the conflicting file and rewrite the assembly descriptor to exclude it
Pascal Thivent
A: 

You are probably having problems with your libraries.. Try doing below.

  1. Build your application JAR File
  2. Get all libraries that you have used for the application and put them in a folder lib.
  3. Place your JAR file and lib folder in a new folder say MyApp.
  4. Open your file by using 7-ZIP or WinRAR. Look for Manifest.MF in the META-INF folder.
  5. Your manifest file should look something like..

Manifest-Version: 1.0 Ant-Version: Apache Ant 1.7.0 Created-By: 1.6.0_03-b05 (Sun Microsystems Inc.) Main-Class: Class-Path: lib/.jar lib/.jar lib/.jar ...

Matt