2015-01-30 2 views
0

MySQL 데이터를 .XLS로 내보내는 작은 응용 프로그램을 만들려고합니다.MySQL에서 DataNucleus (java)를 사용하는 XLS

MySQL의 스키마는 여기에서있다 : http://www.mysqltutorial.org/mysql-sample-database.aspx

일부 소스 : 내 컴퓨터에 MySQL을 DB에 MySQL의 연결을 정의하기 위해 노력하고있어

의 persistence.xml. 그리고 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; 

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(); 
    } 

} 
,691,363를 시작하는 내 기업

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 하나의 메인 클래스의210

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(); 
    } 
} 

첫 번째 경우 (주석 처리 된 부분 참조)에는 DB에서 모든 고객을 읽고 표준 출력에 이름을 씁니다. 이 프로그램은 모든 이름을 suddessfully 씁니다.

하지만, 내가 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 

그러나이, 그 지속을, 그것은 성공적으로 실행됩니다.

몇 가지 예를 Java에서 일부 개체를 xls로 유지할 수 있습니까?

+0

를 추가하기로 결정하기 전에 존재하는 경우 확인 있도록 엑셀에 대한 EMF에 지속성 속성을 datanucleus.attachSameDatastore 설정을 "false"를 추가해야 로그에 뭐라고 써 있니? 그리고 그 객체들을 다른 데이터 저장소에서 유지하려고 시도하기 전에 첫 번째 데이터 저장소에서 분리 했습니까? –

+0

@NeilStockton : 로깅 설정이 없습니다. 나는 그들을 어디에서나 떼어 놓지 않았다. 데이터 저장소에서 데이터를 분리하려면 어떻게해야합니까? –

+0

어떻게 분리 할 수 ​​있습니까? JPA 구현의 문서를 읽으시겠습니까? EM을 닫으십시오. 그리고 문제가 있다면 로깅을 설정하십시오 (다른 방법으로 디버그 할 것으로 예상하기 때문에?) –

답변

1

다른 데이터 저장소에 객체를 부착하고 있기 때문에 각 개체가 이미 그들에게

관련 문제