简体   繁体   English

仅使用数据 JPA 和 CrudRepository 保存不更新

[英]Only save not update using data JPA and CrudRepository

I have a situation in my application that i want to save new records.我的应用程序中有一种情况,我想保存新记录。 I am using Crudrepository save() method.我正在使用 Crudrepository save() 方法。 As we all know what save() will do save if record is not there and update if its found.众所周知,如果记录不存在,save() 将执行什么保存,如果找到则更新。

But i want to restric the Update operation.但我想限制更新操作。 Like if the record is already available neither update not save that record.就像如果记录已经可用,则更新不保存该记录。

Let say below is our Entity class which have composite key.假设下面是我们的实体 class ,它具有复合键。 and based on composite key we want to do this operation.并基于复合键我们想要做这个操作。

Entity class实体 class

@Entity
@IdClass(PrescriptionKey.class)
public class Prescription implements Serializable{

  @GeneratedValue(strategy=GenerationType.AUTO)
  @Column(name="prescription_id")
  private int prescriptionId;

  @Id
  @Column(name="dose_id")
  private int doseId;

  @Id
  @Column(name="med_id")
  private int medId;

  //Setter Getters
}

Composite key class复合键 class

 public class PrescriptionKey implements Serializable {

  private int doseId;

  private int medId;

  //Setter Getter
}

Let say every time the data which comes for save operation contains new data along with the old which was saved already.假设每次用于保存操作的数据都包含新数据以及已保存的旧数据。 Now we want to perform only save for the new data.现在我们只想对新数据执行保存。

Sometimes it may be the prescriptionId which you are trying to update is not found.有时可能是您尝试更新的处方 ID 未找到。 The autogenerated prescriptionId which provides by jpa is smaller than prescriptionId which you are trying to update. jpa 提供的自动生成的处方 ID 小于您尝试更新的处方 ID。 So JPA create a new record with autogenerated prescriptionId instead of prescriptionId which you are trying to update.因此,JPA 使用自动生成的处方 ID 而不是您要更新的处方 ID 创建一个新记录。

This is a good question indeed.这确实是个好问题。 I wonder why there is no such facility in JPA repositories.我想知道为什么 JPA 存储库中没有这样的设施。 Well, for now, what you can do is rely on EntityManager for this.好吧,现在,您可以做的是依靠EntityManager来实现这一点。

entityManager.persist(entityName);

This will assure that only save operation will happen.这将确保只发生保存操作。 Based on the docs - if the entity already saved, it will throw an EntityExistsException .根据文档 - 如果实体已经保存,它将抛出EntityExistsException

Reference : https://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#persist-java.lang.Object-参考: https : //docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#persist-java.lang.Object-

So how the JPA save() works is that if you first find the entity and perform some update to a field(s).因此,JPA save() 的工作原理是,如果您首先找到实体并对字段执行一些更新。 When you pass that same entity back to the save method it will perform an update since the entity had a field or field(s) updated.当您将同一实体传递回 save 方法时,它将执行更新,因为该实体已更新了一个或多个字段。 Therefore if you want to only save without updating you need to create a new entity with the values you want then pass that to the save method.因此,如果您只想保存而不更新,则需要使用所需的值创建一个新实体,然后将其传递给 save 方法。 It will perform an insert provided you not violating unique value constraints for your columns如果您不违反列的唯一值约束,它将执行插入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM