问题描述
研讨会课程:
@Entity
@Table(name =seminars)
公开课讨论会{
@Id
@GeneratedValue
private int id;
@OneToMany(cascade = CascadeType.ALL,mappedBy =seminar)
private List< SeminarParticipation>参股;
...
}
SeminarParticipation code> class:
@Entity
@Table(name =seminar_participations)
public class SeminarParticipation {
@Id
@GeneratedValue
private int id;
@ManyToOne
@JoinColumn(name =seminar_id,nullable = false)
private研讨会研讨会;
...
}
在DAO中,我使用EntityManager来处理研讨会如下:
em.persist(研讨会)
em.merge(研讨会)
em。删除(研讨会)
保存或删除研讨会实体, seminar_participations 中的记录也被创建/删除。 但是,当我尝试更新研讨会在使用 merge 方法设置新的 List< SeminarParticipation> 参与之后),根据列表创建新记录,但旧的不会被删除。
持久代码:
控制器代码(我也尝试更新现有列表,也没有工作):
seminar.setParticipations(newParticipations);
seminarService.updateSeminar(研讨会);
服务等级
@Service
public class SeminarServiceImpl implements SeminarService {
$ b $ @Autowired
private SeminarDAO seminarDAO;
@Override
public void updateSeminar(研讨会研讨会){
seminarDAO.update(研讨会);
}
...
}
3)DAO类
@Repository
public class SeminarDAOImpl implements SeminarDAO {
@PersistenceContext
私人EntityManager em;
@Override
@Transactional
public void update(研讨会研讨会){
em.merge(研讨会);
}
...
}
如何解决这个问题?有没有办法在DAO中手动删除它们?
.objectdb.com / api / java / jpa / OneToMany / orphanRemovalrel =nofollow> orphanRemoval = true @OneToMany参数。I have the following entities:
Seminar class:
@Entity @Table(name = "seminars") public class Seminar { @Id @GeneratedValue private int id; @OneToMany(cascade = CascadeType.ALL, mappedBy = "seminar") private List<SeminarParticipation> participations; ... }
SeminarParticipation class:
@Entity @Table(name = "seminar_participations") public class SeminarParticipation { @Id @GeneratedValue private int id; @ManyToOne @JoinColumn(name="seminar_id", nullable=false) private Seminar seminar; ... }
In DAO, I use EntityManager to work with seminars like this:
em.persist(seminar) em.merge(seminar) em.remove(seminar)
When saving or deleting Seminar entities, records in seminar_participations are created/deleted as well.
But when I try to update a seminar (after setting new List<SeminarParticipation> of participations) using merge method, new records there are created in according to the list, but old ones are not deleted.
Persisting code:
1) Controller code (I've also tried to update the existing list, it does not work as well):
seminar.setParticipations(newParticipations); seminarService.updateSeminar(seminar);
2) Service class
@Service public class SeminarServiceImpl implements SeminarService { @Autowired private SeminarDAO seminarDAO; @Override public void updateSeminar(Seminar seminar) { seminarDAO.update(seminar); } ... }
3) DAO class
@Repository public class SeminarDAOImpl implements SeminarDAO { @PersistenceContext private EntityManager em; @Override @Transactional public void update(Seminar seminar) { em.merge(seminar); } ... }
How I can solve this problem? Is there a way without deleting them manually in DAO?
You should try using orphanRemoval = true parameter for @OneToMany.
这篇关于Hibernate合并不会删除OneToMany旧实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!