从 MySQL 到使用 DataNucleus 的 XLS (java)

From MySQL to XLS with DataNucleus (java)

我正在尝试创建一个小应用程序,以将 MySQL 数据导出到 .XLS。

MySQL 架构来自此处:http://www.mysqltutorial.org/mysql-sample-database.aspx

一些来源:

persistence.xml
我正在尝试在我的计算机上定义到 MySQL 数据库的 MySQL 连接。还有一个 xls 文件。

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_1_0.xsd" version="1.0">
<persistence-unit name="JPA-SAMPLE" transaction-type="RESOURCE_LOCAL">

  <class>com.example.poc.entities.Customer</class>
  <exclude-unlisted-classes/>
  <properties>
    <property name="datanucleus.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
    <property name="datanucleus.ConnectionURL" value="jdbc:mysql://localhost/datanucleustest"/>
    <property name="datanucleus.ConnectionUserName" value="root"/>
    <property name="datanucleus.ConnectionPassword" value=""/>
    <property name="datanucleus.autoCreateSchema" value="true"/>
    <property name="datanucleus.validateTables" value="false"/>
    <property name="datanucleus.validateConstraints" value="false"/>
  </properties>

</persistence-unit>

<persistence-unit name="excel">
  <class>com.example.poc.entities.Customer</class>
  <exclude-unlisted-classes/>

  <properties>
    <property name="javax.persistence.jdbc.url" value="excel:file:tutorial.xls"/>
    <property name="datanucleus.schema.autoCreateAll" value="true"/>
    <property name="datanucleus.schema.validateTables" value="false"/>
    <property name="datanucleus.schema.validateConstraints" value="false"/>
  </properties>
</persistence-unit>

Customer.java
这是我的实体

package com.example.poc.entities;


import lombok.Data;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Data
@Table(name = "CUSTOMERS")
public class Customer {

    @Id
    @Column(name = "customerNumber")
    private Integer id;

    @Column(name = "customerName")
    private String name;

    @Column(name = "contactLastName")
    private String contactLastName;

    @Column(name = "contactFirstName")
    private String contactFirstName;

    //...
    //and so on, all the attributes defined here

}

Main.java 一主class,开始一切

package com.example.poc;

import com.example.poc.daos.CustomerDao;

import javax.persistence.*;

public class Main {

    private static EntityManager mysqlManager;
    private static EntityManager xlsManager;

    public static void main(String[] args) {
        EntityManagerFactory mysqlManagerFactory = Persistence.createEntityManagerFactory("JPA-SAMPLE");
        EntityManagerFactory xlsManagerFactory = Persistence.createEntityManagerFactory("excel");
        mysqlManager = mysqlManagerFactory.createEntityManager();
        xlsManager = xlsManagerFactory.createEntityManager();

        CustomerDao customerDao = new CustomerDao(mysqlManager, xlsManager);
        customerDao.exportAllCustomerToXls();
    }

}

CustomerDao.java
我有一个 DAO class,用于阅读和写作。

package com.example.poc.daos;

import com.example.poc.entities.Customer;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import java.util.ArrayList;
import java.util.List;

public class CustomerDao {

    private EntityManager mysqlManager;
    private EntityManager xlsManager;

    public CustomerDao(EntityManager mysql, EntityManager xls) {
        this.mysqlManager = mysql;
        this.xlsManager = xls;
    }

    public void exportAllCustomerToXls() {
        List<Customer> customers =
                mysqlManager.createQuery("SELECT c FROM Customer c", Customer.class).getResultList();

//        for(Customer c: customers){
//            System.out.println(c.getName());
//        }

        EntityTransaction tx = xlsManager.getTransaction();
        tx.begin();
        for(Customer c : customers){
            xlsManager.persist(c);
        }
        tx.commit();
    }
}

在第一种情况下(请参阅注释部分),我从数据库中读取所有客户,并将他们的姓名写入标准输出。程序突然把他们的名字都写了

但是,当我尝试将它们写入 xls 文件时,我会收到以下错误:

jan. 30, 2015 12:59:25 DU org.datanucleus.store.rdbms.query.ForwardQueryResult closingConnection
INFO: Reading in results for query "SELECT c FROM Customer c" since the connection used is closing/committing
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 14.187 s
[INFO] Finished at: 2015-01-30T12:59:26+01:00
[INFO] Final Memory: 37M/286M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.1:java (default-cli) on project datanucleus-poc: An exception occured while executing the Java class. null: InvocationTargetException: NullPointerException -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

但是,当我手动创建一个 Customer 对象并坚持该对象时,它会成功运行。

你能告诉我一些例子,在 Java 中将一些对象保存到 xls 中吗?

由于您将对象附加到不同的数据存储,因此您需要将持久性 属性 datanucleus.attachSameDatastore 设置为 "false" 添加到 Excel 的 EMF,这样它就会在决定添加之前检查每个对象是否已经存在