0

I'm trying to persist elements using JPA and EclipseLink. So I've created a class to persist

@Entity
public class Couple implements Serializable{
    private static final long serialVersionUID = 1L;
    @Column(name = "OBJECTID")
    private String objectID;
    @Column(name = "EPCNUMBER")
    private String epcNumber;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

And so on. I've created a class to "use" that :

public class Main {
  private static final String PERSISTENCE_UNIT_NAME = "MyPU";
  private static EntityManagerFactory factory;

  public static void main(String[] args) {
    factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    EntityManager em = factory.createEntityManager();
    TypedQuery<Couple> q = em.createQuery("SELECT c FROM Couple c", Couple.class);
    List<Couple> couples = (List<Couple>) q.getResultList();

And then, i've the following persistence.xml :

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="MyPU">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <jta-data-source>localJTA</jta-data-source>
        <class>fr.mypackage.com.Couple</class>
        <properties>
            <property name="eclipselink.ddl-generation" value="create-tables"/>
            <property name="eclipselink.ddl-generation.output-mode" value="database" />
            <property name="eclipselink.logging.level" value="INFO"/>
        </properties>
    </persistence-unit>
</persistence>

But, even when I change the properties, I've got the same error :

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named MyPU (when calling factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);).

Did I do something wrong to ling xml and persistence unit ? I've weel added to classpath the following :

javax.persistence.jar
javax.ejb.jar
eclipselink.jar
javax.persistence_1.0.0.jar
javax.persistence_2.0.4.v201112161009.jar
derby.jar

Could you help me ? Thanks !

ryvantage
  • 13,064
  • 15
  • 63
  • 112
Derbie
  • 413
  • 1
  • 12
  • 28
  • You have too many persistence.jars on there, you should pick on. More information is needed to figure out why the eclipselink provider isn't found. You might add in eclipselink dependencies to your main class to verify the classpath has them. – Chris Apr 15 '13 at 12:06
  • So I removed so og them, I just have the following now : javax.ejb.jar eclipselink.jar javax.persistence_2.0.4.v201112161009.jar derby.jar org.eclipse.persistence.jpa.jpql_1.0.1.jar org.eclipse.persistence.jpa.modelgen.processor-2.3.2.jar But I still have the same.How to check the classpath contains the eclipslink dependencies ? Thanks ! – Derbie Apr 15 '13 at 13:39
  • where did the org.eclipse.persistence.jpa.jpql_1.0.1.jar jar come from? Check that you have the full eclipselink.jar and not renamed one of the OSGI bundles - it should have a META-INF\services\javax.persistence.spi.PersistenceProvider file that lists the provider class name to be used in the tag. Also try initializing an EclipseLink class in your main method before obtaining the persistence unit - it will give you a compile/runtime error if your classpath is set up incorrectly. Something like new org.eclipse.persistence.platform.database.DatabaesPlatform() – Chris Apr 15 '13 at 13:53
  • Ok, so I've done what you told my, I added the right jars and remove the others. I've also initialize an AccessPlatform in my main class : no error. But I still don't have the persistence provider. I've changed javax.persistence.spi.PersistenceProvider in the persistence.xml, should I ? – Derbie Apr 15 '13 at 14:39
  • no, it should be "org.eclipse.persistence.jpa.PersistenceProvider" – Chris Apr 15 '13 at 17:32

1 Answers1

0

On my application, I was having this problem until I compiled my Netbeans project. Then it worked.

ryvantage
  • 13,064
  • 15
  • 63
  • 112