Skip to content
Advertisement

Delete from a JPARepository with an EmbeddedId by a field in the primary key

Currently I have a Spring Boot application using JpaRepository<Employee, EmployeePk> where let’s say EmployeePk is firstname, lastname. Is there a way to delete by Primary Key field without having to specify a custom @Query? It’s ok to delete multiple rows if multiple people were named “John”.

Example:

JavaScript

such as

JavaScript

Employee class and Embedded PK

JavaScript

Advertisement

Answer

Thanks for adding the Employee and EmployeePK code source.

Since you have the field firstName in your entity so you can use derived delete queries that let you avoid having to declare the JPQL query explicitly

Based in spring data documentation

Derived Delete Queries

Spring Data JPA also supports derived delete queries that let you avoid having to declare the JPQL query explicitly, as shown in the following example:

You can add this method in your repositroy.

JavaScript

You don’t need @Modify annotation since you will not use @Query annotation.

And add @Entity to your entity in order to be mapped by the associated table in the database.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement