为 JPA 中的多插入创建自定义/本机查询

问题描述 投票:0回答:0

我正在尝试为 JPA 中的多插入编写本机查询,但遇到问题。

所以基本上我想以以下形式运行查询: 插入表(c1,c2,c3)值(1,2,3),(4,5,6),(7,8,9);

我的模型类是:

public class TimeSeries {

    @EmbeddedId
    private TimeSeriesId id;

    @Column(name = "ts_value", precision = 15)
    @JsonView(Views.Public.class)
    private Double value;

    @Column(name = "original_value")
    @JsonView(Views.Public.class)
    private String originalValue;

    @Column(name = "is_imputed")
    @JsonView(Views.Public.class)
    private boolean isImputed;

    @Column(name = "transaction_id")
    @JsonView(Views.Public.class)
    private UUID transactionId;

    public TimeSeries (TimeSeriesId id, Double value, String originalValue,
                       boolean isImputed, UUID transactionId)
    {
        this.id = id;
        this.value = value;
        this.originalValue = originalValue;
        this.isImputed = isImputed;
        this.transactionId = transactionId;
    }
}
public class TimeSeriesId implements Serializable {

    @Column(name = "time_stamp")
    @JsonView(Views.Public.class)
    private Long time;

    @Column(name = "instrument_id")
    @Type(type = "org.hibernate.type.PostgresUUIDType")
    @JsonView(Views.Public.class)
    private UUID instrumentId;
}

我的Repo中的方法是:

    @Modifying
    @Query(value = "INSERT INTO gem_configurator.timeseries (time_stamp, instrument_id, ts_value, original_value, is_imputed, transaction_id) VALUES (:tsValues)", nativeQuery = true)
    void multiInsertTimeSeries(@Param("tsValues") List<TimeSeries> tsValues);

所以,当我将 TimeSeries 列表(例如,将大小设为 20)发送到 multiInsertTimeSeries 时,查询的形式如下:

插入 gem_configurator.timeseries (time_stamp, instrument_id, ts_value, original_value, is_imputed, transaction_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) 2023-04-03 16:10:08.994 调试 17228 --- [nio-8081-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper:无法执行本机 批量操作查询 [INSERT INTO gem_configurator.timeseries (time_stamp, instrument_id, ts_value, original_value, is_imputed, transaction_id) VALUES (:tsValues_0, :tsValues_1, :tsValues_2, :tsValues_3, :tsValues_4, :tsValues_5, :tsValues_6, :tsValues_7, :tsValues_8, :tsValues_9, :tsValues_10, :tsValues_11, :tsValues_12, :tsValues_13,:tsValues_14,:tsValues_15,:tsValues_16,:tsValues_17, :tsValues_18, :tsValues_19)]

org.postgresql.util.PSQLException:列索引超出范围: 21、列数:20.

它似乎试图插入 TimeSeries 对象的所有字段来代替单个字段?而它应该像 (?,?,?...)

我还通过编写以下查询尝试使用 SPEL:

 @Query(value = "INSERT INTO gem_configurator.timeseries (time_stamp, instrument_id, ts_value, original_value, is_imputed, transaction_id) VALUES (:#{#tsValues.![id.time]}, :#{#tsValues.![id.instrumentId]}, :#{#tsValues.![value]}, :#{#tsValues.![originalValue]}, :#{#tsValues.![isImputed]}, :#{#tsValues.![transactionId]})", nativeQuery = true)

当列表的大小为 1 时似乎运行良好,但对于大于 1 的大小,这也遇到了类似的问题。

有人可以强调我做错了什么吗?

java postgresql spring-boot jpa spring-el
© www.soinside.com 2019 - 2024. All rights reserved.