2017-03-20 5 views
0

테이블로 이동하려는 4 백만 개의 생성 된 엔터티 목록이 있습니다. 엔티티 유형 LocalDateTime과 필드가 :namedParameterJdbcTemplate을 사용하여 LocalDateTime 저장

@Data 
@AllArgsConstructor 

@Entity 
@Table(name = "invoices") 
public class Invoice { 

    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    Long id; 

    @Column(name = "exact_iss_time") 
    private LocalDateTime exactIssueTime; 

    @Column(name = "final_iss_time") 
    private LocalDateTime finalIssueTime; 

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

    @Column(name = "groupid") 
    private Integer groupID; 

    protected Invoice() { 
    } 

} 

그것이 내가 최적를 수행 할 기관의 큰 숫자이기 때문에 - 이런 NamedParameterJdbcTemplate.batchUpdate() 함께 I gues : 나는

public int[] bulkSaveInvoices(List<Invoice> invoices){ 

     String insertSQL = "INSERT INTO invoices VALUES (:id, :exactIssueTime, :finalIssueTime, :issuer, :groupID)"; 
     SqlParameterSource[] sqlParams = SqlParameterSourceUtils.createBatch(invoices.toArray()); 

     int[] insertCounts = namedParameterJdbcTemplate.batchUpdate(insertSQL, sqlParams); 

     return insertCounts; 
    } 

가 계속하지만이 오류 :이 권리를하고

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [INSERT INTO invoices VALUES (?, ?, ?, ?, ?)]; nested exception is org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of java.time.LocalDateTime. Use setObject() with an explicit Types value to specify the type to use. 
  1. 암 - 그렇다면 것은 나는이 LocalDateTime를 해결하는 방법 이 경우에 ssue?
  2. 가장 좋은 방법은 무엇입니까? 을 삽입하면 400 만 개의 테스트 엔티티가 스프링 부트로 테이블에 생성됩니까? 데이터베이스?
+0

를 사용하거나 변환 'LocalDateTime'을 데이터베이스에 삽입하기 전에 'Timestamp'에 저장하십시오. 또는 yuo가 hibernate를 사용하여 JPA를 사용하여 배치를 삽입하고'hibernate0-java8'을 사용하거나 당신이 Hibernate 5.2에 있다면 그냥 삽입 할 수 있다고 가정하십시오. –

답변

1

JPA 2.1은 Java 8 이전에 출시되었으므로 새로운 날짜 및 시간 API를 지원하지 않습니다.

당신은 더 체크 How to persist LocalDate and LocalDateTime with JPA

@Converter(autoApply = true) 
public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> { 

    @Override 
    public Timestamp convertToDatabaseColumn(LocalDateTime locDateTime) { 
     return (locDateTime == null ? null : Timestamp.valueOf(locDateTime)); 
    } 

    @Override 
    public LocalDateTime convertToEntityAttribute(Timestamp sqlTimestamp) { 
     return (sqlTimestamp == null ? null : sqlTimestamp.toLocalDateTime()); 
    } 
} 

를 들어, 변환기를 추가 시도 할 수 있습니다 또는 당신이하지 JDBC 접근 방법을 시도 할 수 있지만 하나가 JDBC 4.2 (그리고 호환 드라이버)를 사용해야 custom JPA batch

+0

아니요, 작동하지 않습니다. 같은 오류. – mCs

+0

이것을 확인하십시오 - [jdbc conversion] (http://stackoverflow.com/questions/25536969/spring-jdbc-postgres-sql-java-8-conversion-from-to-localdate), 나는 더 JPA 접근법을 사용합니다. –

관련 문제