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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25718775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:31:57  来源:igfitidea点击:

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

javamysqljpa

提问by Sarbong

I using using maven simple java project

我使用使用 maven 简单的 java 项目

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

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

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

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

and my persistence.xmlis under src/main/resources

persistence.xml的在下src/main/resources

回答by SparkOn

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

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

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

你必须把它放在 src/main/resources/META-INF

One more thing add

补充一件事

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

to your persistence.xml.

到您的persistence.xml。

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

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

回答by Dmytro Plekhotkin

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

添加<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>