Spring数据JPA复合键重复键记录插入导致更新 [英] spring data jpa composite key duplicate key record insertion resulting in update

查看:817
本文介绍了Spring数据JPA复合键重复键记录插入导致更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有复合键的实体,并且我尝试通过使用spring数据jpa存储库到mysql databse来持久化它,如下所示:

I have one entity having composite key and I am trying to persist it by using spring data jpa repository to mysql databse as given below:

@Embeddable
public class MobileVerificationKey implements Serializable{
private static final long serialVersionUID = 1L;

@Column(name="CUSTOMERID")
private Long customerId;

@Column(name="CUSTOMERTYPE")
private Integer customerType;

@Column(name="MOBILE")
private Long mobile;
@Embeddable
public class MobileVerificationKey implements Serializable{

    private static final long serialVersionUID = 1L;

    @Column(name="CUSTOMERID")
    private Long customerId;

    @Column(name="CUSTOMERTYPE")
    private Integer customerType;

    @Column(name="MOBILE")
    private Long mobile;
//getter and setters
}

实体为

@Entity
@Table(name="mobileverificationdetails")
public class MobileVerificationDetails {

    @EmbeddedId
    private MobileVerificationKey key;

    @Column(name="MOBILETYPE")
    private String mobileType;

    @Column(name="MOBILEPIN")
    private Integer mobilePin;
//getters and setters

}

我的spring data jpa存储库如下所示:

My spring data jpa repository look like this:

public interface MobileVerificationDetailsRepository extends
        CrudRepository<MobileVerificationDetails, MobileVerificationKey> {

    @Override
    MobileVerificationDetails save(MobileVerificationDetails mobileVerificationDetails);

    @Override
    MobileVerificationDetails  findOne(MobileVerificationKey id);
}

现在,如果我尝试为原始记录添加具有相同键的重复记录,并为其他字段添加不同值.当我尝试插入第二条记录时,它将导致使用新值更新现有记录,而不是因为违反主键而引发异常约束...任何人都可以向我解释这种行为.

Now if I am trying to add duplicate record with same key for original record and different values for other fields .when i try to insert second record it results in update of existing record with new values instead of throwing exception for violating primary key constraint...can any one please explain me this behavior.

推荐答案

解决此问题的最简单(且侵入性最小)的方法可能是确保id仅在持久化之前设置好.这可以在@PrePersist回调中实现:

The easiest (and least invasive) way to work around this is probably by making sure the id only gets set right before the persist. This can be achieved in a @PrePersist callback:

abstract class MobileVerificationDetails {

  @EmbeddedId
  private MobileVerificationKey id;

  @PrePersist
  void initIdentifier() {

    if (id == null) {
      this.id = … // Create ID instance here.
    }
  }
}

或者,您可以通过实现Persistable并相应地实现isNew()来强制使用persist(…).确保此方法在第一次插入时返回true.我们通常会看到人们持有瞬态布尔标志,该标志通过@PostPersist/@PostLoad带注释的方法进行了更新.

Alternatively to that you can enforce persist(…) being used by implementing Persistable and implementing isNew() accordingly. Make sure this method returns true on first insert. We usually see people holding a transient boolean flag that is updated in an @PostPersist/@PostLoad annotated method.

abstract class AbstractEntity<ID extends Serializable> implements Persistable<ID> {

  private @Transient boolean isNew = true;

  @Override
  public boolean isNew() {
    return isNew;
  }

  @PostPersist
  @PostLoad
  void markNotNew() {
    this.isNew = false;
  }
}

这篇关于Spring数据JPA复合键重复键记录插入导致更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆