我是一位有用的助手,可以为您翻译内容。以下是需要翻译的IT技术相关内容:
我在使用Spring Data JPA存储库时遇到了一个问题,但我不明白它的原因。我有以下实体:
@Entity
public class Ability {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String color;
private String image;
@ManyToOne(
fetch = FetchType.EAGER
)
private Subject subject;
@OneToMany(
mappedBy = "ability",
cascade = CascadeType.ALL,
orphanRemoval = true,
fetch = FetchType.EAGER
)
private Set<Technology> technologies;
针对这个实体,需要一个JPA存储库。
我不理解的是,当我使用Jpa存储库的deleteById
方法时,记录并没有从数据库中删除。奇怪的是,在使用内存数据库的集成测试中,它可以正常工作。
如果我使用以下代码覆盖deleteById
,则可以正常工作:
@Modifying
@Query("DELETE FROM Ability a WHERE a.id = :id")
void deleteById(@Param("id") Integer id);
但我怀疑我不应该这样做。有没有任何线索表明该方法不能按预期工作?
编辑:添加端点源代码
@RestController
@RequestMapping("/subjects/{subjectId}/abilities")
public class AbilityController {
...
private final AbilityRepository abilityRepository;
private final TechnologyRepository technologyRepository;
private final SubjectRepository subjectRepository;
public AbilityController(AbilityRepository abilityRepository, TechnologyRepository technologyRepository, SubjectRepository subjectRepository) {
this.abilityRepository = abilityRepository;
this.technologyRepository = technologyRepository;
this.subjectRepository = subjectRepository;
}
@DeleteMapping("{abilityId}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteAbility(@PathVariable("subjectId") Integer subjectId, @PathVariable("abilityId") Integer abilityId) {
if (!abilityRepository.existsAbilityBySubjectId(subjectId, abilityId)) {
throw new ResourceNotFoundException(String.format("No ability with Id : %s", abilityId));
}
abilityRepository.deleteById(abilityId);
}
...
}
编辑:添加Hibernate SQL
Hibernate:
select
ability0_.id as id1_0_0_,
ability0_.color as color2_0_0_,
ability0_.image as image3_0_0_,
ability0_.name as name4_0_0_,
ability0_.subject_id as subject_5_0_0_,
subject1_.id as id1_7_1_,
subject1_.icon as icon2_7_1_,
subject1_.image as image3_7_1_,
subject1_.name as name4_7_1_,
technologi2_.ability_id as ability_4_8_2_,
technologi2_.id as id1_8_2_,
technologi2_.id as id1_8_3_,
technologi2_.ability_id as ability_4_8_3_,
technologi2_.image as image2_8_3_,
technologi2_.name as name3_8_3_
from
ability ability0_
left outer join
subject subject1_
on ability0_.subject_id=subject1_.id
left outer join
technology technologi2_
on ability0_.id=technologi2_.ability_id
where
ability0_.id=?
Hibernate:
select
abilities0_.subject_id as subject_5_0_0_,
abilities0_.id as id1_0_0_,
abilities0_.id as id1_0_1_,
abilities0_.color as color2_0_1_,
abilities0_.image as image3_0_1_,
abilities0_.name as name4_0_1_,
abilities0_.subject_id as subject_5_0_1_
from
ability abilities0_
where
abilities0_.subject_id=?
Hibernate:
select
technologi0_.ability_id as ability_4_8_0_,
technologi0_.id as id1_8_0_,
technologi0_.id as id1_8_1_,
technologi0_.ability_id as ability_4_8_1_,
technologi0_.image as image2_8_1_,
technologi0_.name as name3_8_1_
from
technology technologi0_
where
technologi0_.ability_id=?
感谢您提前帮忙。