問題描述:
用jpa刪除數據庫記錄報錯:
org.springframework.orm.jpa.JpaSystemException: Batch update returned unexpected row count from update [0]; actual row count: 2; expected: 1; nested exception is org.hibernate.jdbc.BatchedTooManyRowsAffectedException: Batch update returned unexpected row count from update [0]; actual row count: 2; expected: 1
Caused by: org.hibernate.jdbc.BatchedTooManyRowsAffectedException: Batch update returned unexpected row count from update [0]; actual row count: 2; expected: 1
修改前代碼:
Dao層:
@Repository
public interface FtFileInfoDAO extends JpaRepository<FtFileInfoDO, BigInteger> {
List<FtFileInfoDO> findAllByDataId(Long dataId);
@Transactional
Long deleteByDataId(Long dataId);
}
Entity層:
@Entity
@Table(name = "ft_file_info")
@Data
public class FtFileInfoDO {
@Id
/**
* 數據ID
*/
@Column(name = "data_id")
private Long dataId;
}
調用:
//這個時候id對應兩條記錄
Long fileInfoRes = ftFileInfoDAO.deleteByDataId(id);
解決方法:
只需要修改Entity層,增加id屬性
@Entity
@Table(name = "ft_file_info")
@Data
public class FtFileInfoDO {
@Id
/**
* 主鍵ID
*/
@Column(name = "id")
private Long id;
/**
* 數據ID
*/
@Column(name = "data_id")
private Long dataId;
}