2015-02-02 2 views
0

DataNucleus를 사용하여 .xls 파일을 읽고 쓰고 싶습니다. 아파치 POI에서이 질문을 많이 발견했지만 필요하지 않은 경우 DataNucleus 대신 Apache POI를 직접 사용하고 싶지 않습니다.Datanucleus - 숫자 셀에서 텍스트 값을 가져올 수 없습니다.

@Entity 
@Table(name = "JUSTSTRING") 
public class JustStringEntity { 

    @Id 
    @Column 
    private Integer id; 

    @Column 
    private String name; 
} 

그리고 어떤 방법 .XLS에 기록하고, 거기에서 그것을 읽을 수 :

그래서, 나는 테스트 클래스가 있습니다.

public void exportSomeToXls(){ 
    JustStringEntity e1 = new JustStringEntity(); e1.setId(1); e1.setName("Mr. Salieri"); 
    JustStringEntity e2 = new JustStringEntity(); e2.setId(2); e2.setName("Thomas Angelo"); 

    EntityTransaction tx = xlsManager.getTransaction(); 
    tx.begin(); 
    xlsManager.persist(e1); 
    xlsManager.persist(e2); 
    tx.commit(); 
} 

public void getSomeFromXls(){ 
    try { 
     List<JustStringEntity> entities = xlsManager.createQuery("SELECT j from JustStringEntity j", JustStringEntity.class).getResultList(); 

     System.out.println("SIZE: " + entities.size()); 

     for (JustStringEntity j : entities) { 
      System.out.println(j.getId()); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

.xls 파일에 데이터를 성공적으로 씁니다. JustStringEntity (가) 포함되어 있습니다 만 String 속성이 잘 작동하면

java.lang.IllegalStateException: Cannot get a text value from a numeric cell 
    at org.apache.poi.hssf.usermodel.HSSFCell.typeMismatch(HSSFCell.java:637) 
    at org.apache.poi.hssf.usermodel.HSSFCell.getRichStringCellValue(HSSFCell.java:714) 
    .... 

: 나는 그것을 읽을 때, 나는 다음과 같은 오류가 발생했습니다. 그러나 숫자 데이터가 포함되어 있으면 실패합니다.

Apache POI를 직접 사용하지 않고이 문제를 해결하는 간단한 방법이 있습니까?

+0

숫자 값 (부동 소수점 또는 적분)이있는 스프레드 시트를 사용합니다. PK 필드가 숫자 일 때만입니까? 즉 문자열 PK와 숫자가 아닌 PK를 시도 했습니까? 또한 Long 래퍼보다는 오랫동안 원시 프리미티브로 시도해 보셨습니까? –

+0

기본 유형을 사용하면 작동합니다. 고마워, 그리고 답으로 게시하면 이걸 받아 들일거야. –

답변

0

Long/Integerlong/int (기본형)으로 변경해보세요.

나는 여전히 원시 래퍼와 함께 작동 할 것이라고 기대했지만 어쩌면 거기에/그 특정 경우에 대한 버그가 있습니까?

+0

'@ Id '가 원시 타입에 대한 래퍼 클래스이면 실패합니다. '@ Id '가 String이거나 기본 유형이면 작동합니다. 다른 필드가 래퍼 클래스 인 경우에도 작동합니다. –

관련 문제