Search code examples
spring-mvcjpaspring-dataspring-data-jpajpql

Spring Data JPA method for deleting using a two fields in the embaddable key


THis is the main entity class, which is having an embeddedId

public class LabResHivMutation implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private LabResHivMutationPK id;

    private String comments;

    @Column(name="MUTATION_TYPE_ID")
    private BigDecimal mutationTypeId;

    @Column(name="VALUE")
    private String value;   

}

This is the embeddable key

@Embeddable
public class LabResHivMutationPK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    @Column(name="TO_INST")
    private Long toInst;

    @Column(name="REL_INVSTID")
    private long relInvstid;

    @Column(name="MUTATION_ID")
    private long mutationId;

}

Is there any delete methos available in spring data Jpa to delete based on only two of the embaddable key(toInst,relInvstid).

I still can write a JPQL query to delete it. My question is there any method available for this.

like deleteById ?


Solution

  • Yes there is, repo.deleteByIdToInstAndIdRelInvstid(toInst,relInnvstid) As you see you have to specify deleteByIdToInst , this is how you reference a field of an embedded ID , the same as you would reference a field of a foreign relation. Here Id matches your field naming

    @EmbeddedId
    private LabResHivMutationPK id;