我有以下实体:
Seminar
上课:
@Entity
@Table(name = "seminars")
public class Seminar {
@Id
@GeneratedValue
private int id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "seminar")
private List<SeminarParticipation> participations;
...
}
SeminarParticipation
上课:
@Entity
@Table(name = "seminar_participations")
public class SeminarParticipation {
@Id
@GeneratedValue
private int id;
@ManyToOne
@JoinColumn(name="seminar_id", nullable=false)
private Seminar seminar;
...
}
在DAO中,我使用EntityManager来处理这样的研讨会:
em.persist(seminar)
em.merge(seminar)
em.remove(seminar)
保存或删除Seminar
个实体时,也会创建/删除seminar_participations
中的记录。
但是当我尝试使用List<SeminarParticipation>
方法更新研讨会(在设置新的merge
参与之后)后,根据列表创建新记录,但不删除旧记录。
坚持代码:
1)控制器代码(我也试图更新现有列表,但它也不起作用):
seminar.setParticipations(newParticipations);
seminarService.updateSeminar(seminar);
2)服务类
@Service
public class SeminarServiceImpl implements SeminarService {
@Autowired
private SeminarDAO seminarDAO;
@Override
public void updateSeminar(Seminar seminar) {
seminarDAO.update(seminar);
}
...
}
3)DAO课程
@Repository
public class SeminarDAOImpl implements SeminarDAO {
@PersistenceContext
private EntityManager em;
@Override
@Transactional
public void update(Seminar seminar) {
em.merge(seminar);
}
...
}
我如何解决这个问题?有没有办法在DAO中手动删除它们?