StudentServiceImpl.java

@Service("studentServiceImpl")
public class StudentServiceImpl implements StudentService {
	@Autowired
	private StudentRepository studentRepository;
	@Transactional
	public List<Student> saveAllStudent(List<Student> studentList) {
		List<Student> response = (List<Student>) studentRepository.saveAll(studentList);
		return response;
	@Transactional
	public void deleteInBatch(List<Student> studentList) {
		studentRepository.deleteInBatch(studentList);

Repository

在service层调用studentRepository的deleteInBatch方法,StudentRepository之所以有这个方法,是因为它继承了JpaRepository

StudentRepository.java – interface

@Repository
public interface StudentRepository extends JpaRepository<Student, Serializable> {

Controller

最后去controller调用service的方法

StudentController.java

@RestController
@RequestMapping(value = "/student")
public class StudentController {
	@Autowired
	private StudentService studentService;
	@RequestMapping(value = "/deleteinbatch", method = RequestMethod.DELETE)
	@ResponseBody
	public String deleteInBatch(@RequestBody List<Student> studentList) {
		studentService.deleteInBatch(studentList);
		return "All Students deleted successfully";
                    jpa批量删除可以调用JpaRepository的deleteInBatch(Iterable&lt;T&gt; entities)方法,同时把实体列表传参给这个方法。ServiceStudentServiceImpl.java@Service("studentServiceImpl")public class StudentServiceImpl implements StudentService {	@Autowired	private StudentRepository student
在Spring Boot中使用JpaRepository的deleteById(ID id)方法删除数据时,首先要使用existsById(ID id)方法判断数据是否存在。如果存在,再删除。否则,删除一个id不存在的数据会抛出org.springframework.dao.EmptyResultDataAccessException异常:
2021-02-02 09:22:12.587 ERROR 13108 --- [  XNIO-1 task-2] c.a.s.b.e.GlobalExcept
原因分析:deleteInBatch实际转换为如下的sql
delete from [table_name] where [criteria] = id or [criteria] = id (and so on...)
HqlSqlBaseWalker需要搜索遍历所有的where条件语句,如图报错中执行了1020次导致溢出错误。
解决方案:将集合划分为多个小的集合分次调用deleteInBatch
				
JPA 中,可以使用 EntityManager 的 createQuery 方法来创建一个 DELETE 查询,然后调用 executeUpdate 方法来执行批量删除操作。 String hql ="DELETE FROM Entity e WHERE e.id IN :ids"; Query query = em.createQuery(hql); query.setParame...
文章目录前言一、问题定位二、内存溢出原理三、其他尝试1、循环遍历删除delete2、分批次删除3、直接全部删除`gempUserOperationLogDao.deleteAll();`四、解决方案总结 数据库日志库定时清理日志数据,几天之后发现,jvm内存溢出现象。 一、问题定位 1、日志数据量太大(近40w数据) 2、源码分析 @Scheduled(cron = "0 0 1 * * ?") public void timedPullNewInfo() { Date date=new Dat
package org.springframework.data.repository; import org.springframework.stereotype.Indexed; @Indexed public interface Repository<T, ID> { <T>是...
JPA是一种持久层规范,常见的实现有Hibernate、Toplink等。JPA是一种基于ORM思想的规范,以Hibernate为例,在设计上将表和实体类,表字段和实体类字段一一对应,这样就可以直接通过操作实体类来操作数据库表了。 创建实体类 基本上每个表都会对应一个实体类,在创建表对应的实体类时: 1、在实体类上使用@Entity注解来声明该实体类; 2、该实体类的名称不用和表名一样,只...