Sarbong
Sarbong

Reputation: 111

org.apache.openjpa.persistence.ArgumentException while running the main class

I using using maven simple java project

Im getting the following exception while running the main class

346  INFO   [main] openjpa.Runtime - OpenJPA dynamically loaded the class
            enhancer. Any classes that were not enhanced at build time
             will be enhanced when they are loaded by the JVM.
414  INFO   [main] openjpa.Runtime - Starting OpenJPA 2.3.0
<openjpa-2.3.0-r422266:1540826 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:643)
    at org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:203)
    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 org.msharma.JpaImpl.main(JpaImpl.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

My entity clas

@Entity(name ="customer")
public class Customer implements Serializable{
    @Id
    @Column(name ="cust_id", nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long custId;
    @Column(name = "first_name",length = 50, nullable = false)
    private String firstName;
    @Column(name = "last_name",length = 50)
    private String lastName;
    // By default column name is same as attribute name
    private String street;
    private String appt;
    private String city;
    @Column(name = "zip_code",length = 50, nullable = false)
    private String zipCode;
    @Column(name = "cust_type",length = 50, nullable = false)
    private String custType;
    @Version
    @Column(name = "last_updated_time")
    private Date updatedTime;
    public Customer(){}
 // getters and setters
}

This is my persistence.xml

<?xml version="1.0"?>
<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="testjpa" transaction-type="RESOURCE_LOCAL">
        <provider>
             org.apache.openjpa.persistence.PersistenceProviderImpl
        </provider>
        <class>org.somepack.Customer</class>
        <properties>
                <property name="openjpa.ConnectionURL" 
                           value="jdbc:mysql://localhost:3306/test"/>
                <property name="openjpa.ConnectionDriverName" 
                           value="com.mysql.jdbc.Driver"/>
                <property name="openjpa.ConnectionUserName" value="root"/>
                <property name="openjpa.ConnectionPassword" value="pass"/>
                <property name="openjpa.Log" value="SQL=TRACE"/>
        </properties>
    </persistence-unit>
</persistence>

This is my mainClass

 public static void main(String[] args) {
 try {
     EntityManagerFactory entityManagerFactory = Persistence
                                      .createEntityManagerFactory("testjpa");
     EntityManager entityManager = entityManagerFactory.createEntityManager();
     EntityTransaction entityTransaction = entityManager.getTransaction();
     entityTransaction.begin();
     Customer customer = new Customer();
     //set the properties to the customer object
     entityManager.persist(customer);
     entityTransaction.commit();
     entityManager.close();
     entityManagerFactory.close();
    }
 catch (Exception e){
   e.printStackTrace();
 }
}

How do I resolve the problem .

I have openjpa, openjpa-persistence-jdbc, mysql-connector-java dependencies in my POM.xml

and my persistence.xml is under src/main/resources

Upvotes: 0

Views: 14298

Answers (2)

SparkOn
SparkOn

Reputation: 8956

As you said your persistence.xml is under src/main/resources so may be it is unable to read it

you must place it under src/main/resources/META-INF

One more thing add

<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>

to your persistence.xml.

If you add the openjpa.jdbc.SynchronizeMappings property as shown below OpenJPA will auto-create all your tables, all your primary keys and all foreign keys exactly to match your objects

Upvotes: 3

Dmytro Plekhotkin
Dmytro Plekhotkin

Reputation: 1993

Add <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" /> to your persistence.xml:

<?xml version="1.0"?>
<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="testjpa" transaction-type="RESOURCE_LOCAL">
        <provider>
             org.apache.openjpa.persistence.PersistenceProviderImpl
        </provider>
        <class>org.somepack.Customer</class>
        <properties>
                <property name="openjpa.ConnectionURL" 
                           value="jdbc:mysql://localhost:3306/test"/>
                <property name="openjpa.ConnectionDriverName" 
                           value="com.mysql.jdbc.Driver"/>
                <property name="openjpa.ConnectionUserName" value="root"/>
                <property name="openjpa.ConnectionPassword" value="pass"/>
                <property name="openjpa.Log" value="SQL=TRACE"/>
                <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        </properties>
    </persistence-unit>
</persistence>

Upvotes: 0

Related Questions