3

I have a situation in my application that i want to save new records. I am using Crudrepository save() method. As we all know what save() will do save if record is not there and update if its found.

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. and based on composite key we want to do this operation.

Entity 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

 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.

Sam
  • 181
  • 2
  • 4
  • 17
  • The update will happen if the id has value (if the id is null, it means this is a new object). Therefore, why you don't remove objects which have id from the list? – Hamid Ghasemi Dec 08 '18 at 08:01
  • i think if we do so. the record will be duplicated and saved with different Id. – Sam Dec 08 '18 at 08:10
  • Absolutely if you just make id to null, it will duplicate data. I said before calling the `save()` method, remove objects which contain id from the input list. If you add your code to question, I can explain it by code. – Hamid Ghasemi Dec 08 '18 at 08:23
  • Possible duplicate of [Spring JPA repository: prevent update on save](https://stackoverflow.com/questions/35817584/spring-jpa-repository-prevent-update-on-save) – Hamid Ghasemi Dec 08 '18 at 09:01
  • @hamidghasemi i understood what you are saying how we can achieve that if we have our Id as the composite Key. – Sam Dec 08 '18 at 09:33

3 Answers3

1

Sometimes it may be the prescriptionId which you are trying to update is not found. The autogenerated prescriptionId which provides by jpa is smaller than prescriptionId which you are trying to update. So JPA create a new record with autogenerated prescriptionId instead of prescriptionId which you are trying to update.

dasunse
  • 2,839
  • 1
  • 14
  • 32
0

This is a good question indeed. I wonder why there is no such facility in JPA repositories. Well, for now, what you can do is rely on EntityManager for this.

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.

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

Vishal_Kotecha
  • 481
  • 5
  • 19
0

So how the JPA save() works is that if you first find the entity and perform some update to a field(s). 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. 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. It will perform an insert provided you not violating unique value constraints for your columns