运行主类时org.apache.openjpa.persistence.ArgumentException [英] org.apache.openjpa.persistence.ArgumentException while running the main class

查看:163
本文介绍了运行主类时org.apache.openjpa.persistence.ArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用maven简单的Java项目

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)

我的实体分类

@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

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>

这是我的主要班级

 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依赖项

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

而我的persistence.xmlsrc/main/resources

推荐答案

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

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

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

再添加一件事

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

到您的persistence.xml.

to your persistence.xml.

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

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

这篇关于运行主类时org.apache.openjpa.persistence.ArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆