Tôi có một số đối tượng mà tôi không thể xóa và phải cập nhật một trường chung có tên 'đã xóa' thay vì nó. Tôi đọc số there để tôi có thể viết truy vấn chung, sử dụng #{#entityName}
. Vì lý do đó tôi đã cố gắng để ghi đè CrudRepository#delete(…)
phương pháp như thế này:Làm thế nào để ghi đè lên một phương pháp xóa trên một kho dữ liệu Spring CrudRepository?
public interface DeleteableRepository<T, ID extends Serializable> extends CrudRepository<T,ID>{
@Override
@Query("UPDATE #{#entityName} x set x.deleted = 1 where x.id = ?1")
public void delete(ID id);
}
Nhưng tôi có một bài kiểm tra đơn vị cho thấy tôi sai!
@Test
public void testDelete() {
SomeDeleteableObject sdo = new SomeDeletableObject();
sdo = getDeleteableRepository().create(sdo);
Assert.assertNotNull(sdo);
Assert.assertNotNull(sdo.getId());
Assert.assertFalse(sdo.isDeleted());
getDeleteableRepository().delete(sdo);
sdo = getDeleteableRepository().findOne(sdo.getId());
//Fails here
}
Không thể ghi đè CrudRepository
các phương pháp như vậy?
Tại sao bạn muốn sửa đổi hành vi xóa kho lưu trữ và không xử lý tình trạng này trong lớp dịch vụ của bạn? – gipinani
vì nếu tôi có thể xử lý điều này trên Lớp lưu trữ, tôi không cần triển khai Dịch vụ bổ sung hoặc Daos. –