从MySQL到DataNucleus的XLS(java)

我正在尝试创build一个小应用程序,将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一个主要类,开始一切

 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课,去做阅读,写东西。

 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 

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

你能告诉我一些例子,坚持一些对象到Java在XLS?

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

Interesting Posts