🎓 Check Out My Top 25 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare
Create JPA Entity
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
@Entity
@Table(name = "persons")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
}Create Spring Data JPA Repository - PersonRepository
Let's create an PersonRepository interface that extends the JpaRepository interface from Spring Data JPA:
import com.springdatajpa.springboot.entity.Person;
import org.springframework.data.jpa.repository.JpaRepository;
public interface PersonRepository extends JpaRepository<Person, Long> {
}Deleting Entities by List of IDs
void deleteByIdIn(List<Long> ids);Implementing in the Service Layer
@Service
public class PersonService {
@Autowired
private PersonRepository personRepository;
@Transactional
public void deletePersonsByListOfIds(List<Long> ids) {
personRepository.deleteByIdIn(ids);
}
}Safety Considerations
Testing the Implementation
@SpringBootTest
public class PersonServiceTest {
@Autowired
private PersonService personService;
@Autowired
private PersonRepository personRepository;
@Test
public void testDeleteByListOfIds() {
// Given: Initial data
Person john = personRepository.save(new Person("John", "123 Elm Street"));
Person jane = personRepository.save(new Person("Jane", "456 Maple Avenue"));
List<Long> idsToDelete = Arrays.asList(john.getId(), jane.getId());
// When: Deleting persons by IDs
personService.deletePersonsByListOfIds(idsToDelete);
// Then: Assert that the persons are deleted
assertTrue(personRepository.findById(john.getId()).isEmpty());
assertTrue(personRepository.findById(jane.getId()).isEmpty());
}
}Conclusion
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment