运行主类时的org.apache.openjpa.persistence.ArgumentException

时间:2014-09-08 06:50:46

标签: java mysql jpa

我使用maven简单java项目

我在运行主类时遇到以下异常

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)

我的实体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
}

这是我的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>

这是我的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();
 }
}

如何解决问题。

我的POM.xml中有openjpa,openjpa-persistence-jdbc,mysql-connector-java依赖项

我的persistence.xml位于src/main/resources

之下

2 个答案:

答案 0 :(得分:3)

正如您所说,您的persistence.xml位于src/main/resources下,因此可能无法读取它

您必须将其放在src/main/resources/META-INF

还有一件事

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

到你的persistence.xml。

如果您添加openjpa.jdbc.SynchronizeMappings属性,如下所示,OpenJPA将自动创建所有表格,所有主键和所有外键完全匹配您的对象

答案 1 :(得分:0)

<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />添加到 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>