Spring JPA / Hibernate transaction force insert instead of update

后端 未结 3 1407
小蘑菇
小蘑菇 2021-01-04 11:13

Edited. Whilst extending the base repository class and adding an insert method would work an more elegant solution appears to be implementing Persistable in the entities.

3条回答
  •  盖世英雄少女心
    2021-01-04 11:52

    Does this help?

    Set updatable = false on your PK column definition. Example:

    @Id
    @GeneratedValue
    @Column(name = “id”, updatable = false, nullable = false)
    private Long id;
    

    Setting your id non updatable will stop JPA from doing updates on your primary key, so thing about it.

提交回复
热议问题