spring数据jpa:批插入不起作用

93ze6v8z  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(328)

我使用本文为我的一个实体用spring数据jpa实现了批插入。我使用的是mssqldb。
我将主键生成策略从 GenerationType.IDENTITYGenerationType.SEQUENCE :

@Entity
@Table(name = "Answers")
public class AnswerDMO implements java.io.Serializable {
    private static final long serialVersionUID = 1L;
    private long id;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "answer_new_generator")
    @SequenceGenerator(name = "answer_new_generator", sequenceName = "answer_new_sequence", allocationSize = 15)
    @Column(name = "Id", unique = true, nullable = false)
    public long getId() {
        return this.id;
    }
    public void setId(long id) {
        this.id = id;
    }

服务方法通过调用 <S extends T> List<S> save(Iterable<S> entities); :

@Transactional
public List<AnswersDMO> saveAnswers () {
       /* other logic..... */
       if(!CollectionUtils.isEmpty(answersToSave)) {
            supplementalAnswerNewDAO.save(answersToSave);
        }
}

为了告诉hibernate在插入时使用批处理并检查它是否实际工作,我设置了以下jpa属性:

<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.hibernate.jdbc.batch_size">5</prop>
<prop key="hibernate.order_inserts">true</prop>

但是,在插入11个实体时,jdbc批处理计数为0:

(Log4j2-TF-1-AsyncLogger[AsyncContext@44f54c5e]-1)     0 nanoseconds spent releasing 0 JDBC connections;
(Log4j2-TF-1-AsyncLogger[AsyncContext@44f54c5e]-1)     65217828 nanoseconds spent preparing 6 JDBC statements;
(Log4j2-TF-1-AsyncLogger[AsyncContext@44f54c5e]-1)     727057697 nanoseconds spent executing 6 JDBC statements;
(Log4j2-TF-1-AsyncLogger[AsyncContext@44f54c5e]-1)     0 nanoseconds spent executing 0 JDBC batches;

休眠查询:

08:02:35,739 INFO  [stdout] (default task-1) Hibernate: 
08:02:35,740 INFO  [stdout] (default task-1)     select
08:02:35,742 INFO  [stdout] (default task-1)         next value for supplemental_answer_new_sequence

08:02:57,941 INFO  [stdout] (default task-1) Hibernate: 
08:02:57,941 INFO  [stdout] (default task-1)          insert 
08:02:57,941 INFO  [stdout] (default task-1)         into
08:02:57,941 INFO  [stdout] (default task-1)             my_schema.dbo.Answers
08:02:57,941 INFO  [stdout] (default task-1)             (CreatedDate, InstanceId, ProfileId, QuestionId, UpdatedDate, Value, Id) 
08:02:57,941 INFO  [stdout] (default task-1)         values
08:02:57,941 INFO  [stdout] (default task-1)             (?, ?, ?, ?, ?, ?, ?)

有人能告诉我为什么它不起作用吗?
问题2:另外,在我从 GenerationType.IDENTITYGenerationType.SEQUENCE ,我开始在日志中发现以下异常:

[jdbc.spi.SqlExceptionHelper] – SQL Error: 544, SQLState: S0001
[jdbc.spi.SqlExceptionHelper] – Cannot insert explicit value for identity column in table 'Answers' when IDENTITY_INSERT is set to OFF.

是因为 id 列具有 identity 财产?如果是,解决的步骤是什么?
更新:我刚刚注意到我有一个错误的财产名称 <prop key="hibernate.hibernate.jdbc.batch_size">5</prop> ,应该是 <prop key="hibernate.jdbc.batch_size">5</prop> .
所以现在的主要问题是

[jdbc.spi.SqlExceptionHelper] – SQL Error: 544, SQLState: S0001
[jdbc.spi.SqlExceptionHelper] – Cannot insert explicit value for identity column in table 'Answers' when IDENTITY_INSERT is set to OFF.

有什么办法可以解决吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题