0

I have this code:

    <?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="project-unit" transaction-type="RESOURCE_LOCAL">
        <class>model.Product</class>
        <properties>
            <property name="openjpa.ConnectionDriverName" value="org.postgresql.Driver" />
            <property name="openjpa.ConnectionUserName" value="postgres" />
            <property name="openjpa.ConnectionPassword" value="xxx" />
            <property name="openjpa.ConnectionURL" value="jdbc:postgresql://localhost:5432/project" />
            <property name="openjpa.jdbc.DBDictionary" value="postgres" />
            <property name="openjpa.jdbc.SynchronizeMappings"
                                                     value="buildSchema(SchemaAction=add,ForeignKeys=true)" />
            <property name="openjpa.Log" value="DefaultLevel=WARN, Tool=INFO" />
        </properties>
    </persistence-unit>
</persistence>

This main

import model.*;

import javax.persistence.*;

public class pruebaMain {

    public static void main(String [] args){

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("project-unit");
        EntityManager em = emf.createEntityManager();

        Product product = new Product();
        product.setName("cheese");
        product.setCode(21421);
        product.setDescription("Test description");

        EntityTransaction tx= em.getTransaction();
        tx.begin();
        em.persist(product);
        tx.commit();

        em.close();
        emf.close();

    }

}

and this product class: package model;

import javax.persistence.*;

@Entity
public class Product {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
long code;
@Column(nullable=false,length=50)
String name;
@Column(nullable=false,length=500)
String description;
@Column(nullable=false)
int stock;

//builders
public Product(){
    this.code=0;
    this.name="";
    this.description="";
    this.stock=0;
}
public Product(long code, String name, String description,int stock){

    this.code=code;
    this.name=name;
    this.name=description;
    this.stock=stock;
}

//get & set
public long getCode() {
    return code;
}

public void setCode(long code) {
    this.code = code;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public int getStock() {
    return stock;
}

public void setStock(int stock) {
    this.stock = stock;
}



}

But , when I try the code , I have this error:

Exception in thread "main" <openjpa-2.4.0-nonfinal-1598334-r422266:1599166 fatal user error> org.apache.openjpa.persistence.ArgumentException: The persistence provider is attempting to use properties in the persistence.xml file to resolve the data source. A Java Database Connectivity (JDBC) driver or data source class name must be specified in the openjpa.ConnectionDriverName or javax.persistence.jdbc.driver property. The following properties are available in the configuration: "org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl@442ce698". 
    at org.apache.openjpa.jdbc.schema.DataSourceFactory.newDataSource(DataSourceFactory.java:72)
    at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.createConnectionFactory(JDBCConfigurationImpl.java:849)
    at org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl.getDBDictionaryInstance(JDBCConfigurationImpl.java:602)
    at org.apache.openjpa.jdbc.meta.MappingRepository.endConfiguration(MappingRepository.java:1518)
    at org.apache.openjpa.lib.conf.Configurations.configureInstance(Configurations.java:535)
    at org.apache.openjpa.lib.conf.Configurations.configureInstance(Configurations.java:460)
    at org.apache.openjpa.lib.conf.PluginValue.instantiate(PluginValue.java:121)
    at org.apache.openjpa.conf.MetaDataRepositoryValue.instantiate(MetaDataRepositoryValue.java:68)
    at org.apache.openjpa.lib.conf.ObjectValue.instantiate(ObjectValue.java:83)
    at org.apache.openjpa.conf.OpenJPAConfigurationImpl.newMetaDataRepositoryInstance(OpenJPAConfigurationImpl.java:967)
    at org.apache.openjpa.conf.OpenJPAConfigurationImpl.getMetaDataRepositoryInstance(OpenJPAConfigurationImpl.java:958)
    at org.apache.openjpa.kernel.AbstractBrokerFactory.makeReadOnly(AbstractBrokerFactory.java:642)
    at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:202)
    at org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:155)
    at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:226)
    at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:153)
    at org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:59)
    at pruebaMain.main(pruebaMain.java:10)

I dont know what is the problem . I specified the ConnectionDriverName and I added to build path : postgresql-jdbc4.jar , postgresql-9.3-1100.jdbc41.jar

Thanks you!

1 Answer 1

0

I resolved it . I had the persistence.xml in a wrong folder, now it is in META-INF and it works!

1
  • You should not need to specify openjpa.ConnectionDriverName anymore. JDBC should find it automatically for you these days, based upon the connection URL.
    – Steve C
    Jun 4, 2015 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.