Rosh
Rosh

Reputation: 1736

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 ?

Upvotes: 2

Views: 9893

Answers (3)

Manoj meghwal
Manoj meghwal

Reputation: 1

User underscore '_' when entity will have multiple keys with using @Embedded keys. example :repository.deleteByid_toInst();

Upvotes: 0

Mustafa Kannan
Mustafa Kannan

Reputation: 121

There are two ways to delete an entity: either using its own "JPA Repository derived delete method" long deleteByFirstIdAndSecondId(long firstId , secondId)
In your service you can simply call it : repository.deleteByFirstIdAndSecondId(long firstId , secondId)

Another way is through the parent entity by excluding the child entity (or entities depends on the relation type).

Upvotes: 0

Paul Vlasin
Paul Vlasin

Reputation: 314

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;

Upvotes: 4

Related Questions