@OneToMany实体更改时,有什么方法可以调用@PreUpdate吗?

时间:2019-08-03 17:23:22

标签: java spring hibernate jpa

当我向@OneToMany集合中添加新实体时,我想调用@PreUpdate。 我还看到了其他问题,但对我而言不起作用。

我尝试使用@PreUpdate注释实体方法,但仅在更改公共属性(不是OneToOne,OneToMany等...,例如字符串等)时才触发。

将辅导员与小组联系起来的方法,位于小组课中。

@PrePersist
@PreUpdate
private void associate() {
    if (this.getCounselors() == null)
        return;

    this.getCounselors().forEach(teacher ->
            teacher.setEducatedGroup(this));
}

服务中的方法

@Transactional
public Teacher saveGroupCounselor(Integer id, Teacher teacher) {
    Group group = groupRepository.findById(id).orElseThrow(() ->
            new ResourceNotFoundException("Group not found"));

    if (teacher == null || teacher.getId() == null)
        throw new IdentifierNotValidException("Teacher identifier not specified");

    Teacher checkedTeacher = teacherRepository.findById(teacher.getId()).orElseThrow(() ->
            new ResourceNotFoundException("Teacher not found"));

    group.addCounselor(checkedTeacher);
    return checkedTeacher;
}

正如我前面提到的,@PreUpdate仅在更新诸如Strings之类的通用属性时才会触发,但是我想在向集合中添加新实体时调用@PreUpdate。有什么办法可以实现?

0 个答案:

没有答案