2017-03-20 11 views
0

私は、テーブルに移動したい400万の生成されたエンティティのリストを持っています。私は入れませんがLocalDateTimeをnamedParameterJdbcTemplateで保存します。

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

:私guesはこのようなNamedParameterJdbcTemplate.batchUpdate()である - それは私が最適にそれをしたいエンティティの大きな数であるとして

@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() { 
    } 

} 

:エンティティは、タイプLocalDateTimeでフィールドを持っていますエラー:

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万のテストエンティティがSpringブートでテーブルに生成されますか? データベース?
+0

を使用することができるため、コンバータを追加してみてください'LocalDateTime'をデータベースに挿入する前に' Timestamp'に設定してください。または、yuoが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変換](http://stackoverflow.com/questions/25536969/spring-jdbc-postgres-sql-java-8-conversion-from-to-localdate)、私はより多くのJPAのアプローチを使用して –

関連する問題