You mainly want to derive on Spring Data JPA
s derived query features as much as possible! You definitely don't want to write your own boilerplate code because that defeats the purpose of Spring Data JPA
entirely!
Delete
The best (and cleanest) way to do this is to let Spring Data JPA
derive the query for you:
public interface EmployeeRepository extends CrudRepository<Employee, Long> {
Long deleteByIdIn(List<Long> employeeIdsToDelete);
}
See this answer.
Update
Updates can be done this way:
@Modifying
@Query("update Employee empl set empl.param =:param where empl.id =:emplId")
void updateEmployees(@Param("emplId") Long emplId, @Param("param") paramValue);
And otherwise can be done with a combination of find and modify (likely in the Service
method:
public void myEmployeeServiceUpdateMethod(List<Long> emplIds) {
List<Employee> employees = employeeRepository.findByIdIn(emplIds);
employees.forEach(empl -> empl.setParam("NewParam!");
}
And Spring JPA will automatically flush and update those values for you!
See this answer.
Create
Creates you're already doing right. Just pass in the list of new entities to #save
.