我是新的Hibernate / JPA。从父元素中删除子元素时遇到问题。下面是我的实体类SourceMaster和SegmentMaster。
@Entity
public class SourceMaster implements Serializable{
@EmbeddedId
private SourceMasterKey id;//Primary Key is composite key. Combination of SOURCE_ID&DOMAIN_ID
@OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER, mappedBy = "sourceTable") //I don't want to delete children
private List<SegmentMaster> segmentList = new ArrayList<>();
}
@Entity
public class SegmentMaster implements Serializable{
@EmbeddedId
private SegmentMasterKey id;//Primary Key is composite key. Combination of SEGMENT_NAME&CA_DOMAIN_ID
@ManyToOne (optional = false) //FK to SourceMaster table
@JoinColumns({
@JoinColumn(name = "SOURCE_ID", nullable = false, referencedColumnName = "SOURCE_ID", insertable = false, updatable = false),
@JoinColumn(name = "DOMAIN_ID", nullable = false, referencedColumnName = "DOMAIN_ID", insertable = false, updatable = false)})
private SourceMaster sourceTable;
}
委托课程
public class SourceMasterDelegate{
sourceMasterDAO.delete(SourceMaster); // Works as intended, returns me integrity constraint violated - child record found
}
public class SegmentMasterDelegate{
sourceMasterDAO.delete(SegmentMaster);//Problem here. It is not deleting SegmentMaster record, not giving exception or not logging any exception.
//It executes select statements on SegmentMaster and SourceMaster tables. but delete Query executing is never seen.
}
public class HDAOImpl implements GenericDAO<T> {
@Transactional
public void delete(T object) {
entityManager.remove(object);
entityManager.flush();
}
关联有什么问题吗?为什么我在这种情况下无法删除子记录?
这是我得到的最接近的答案,但这不起作用或我做错了可能是 - &gt; Using JPA with Hibernate implementation: entityManager.remove - not working
任何指针都会很棒!请建议