这是我的代码,
SiteMember 类
@OneToMany(mappedBy = "member",cascade=CascadeType.ALL)
private List<MemberThread> memberThread = new ArrayList<MemberThread>
和
成员线程类
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(nullable=false)
private SiteMember member;
MemberThreadJpaController 类
public void destroy(Long id) throws IllegalOrphanException, NonexistentEntityException, NotSupportedException, RollbackException, HeuristicMixedException, HeuristicRollbackException, SecurityException, IllegalStateException, SystemException {
EntityManager em = null;
try {
em = getEntityManager();
ut.begin();
MemberThread memberThread;
try {
memberThread = em.getReference(MemberThread.class, id);
memberThread.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The memberThread with id " + id + " no longer exists.", enfe);
}
List<String> illegalOrphanMessages = null;
List<Reply> replysOrphanCheck = memberThread.getReplys();
for (Reply replysOrphanCheckReply : replysOrphanCheck) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("This MemberThread (" + memberThread + ") cannot be destroyed since the Reply " + replysOrphanCheckReply + " in its replys field has a non-nullable memberThread field.");
}
if (illegalOrphanMessages != null) {
throw new IllegalOrphanException(illegalOrphanMessages);
}
SiteMember member = memberThread.getMember();
if (member != null) {
member.getMemberThread().remove(memberThread);
member = em.merge(member);
}
em.remove(memberThread);
ut.commit();
} finally {
if (em != null) {
em.close();
}
}
}
这将删除 SiteMember 和 MemberThread 对象,因为 MemberThread 具有 SiteMember 的外键。我怎样才能只删除 MemberThread?
谢谢..