JPA规范是不支持级联删除, 不过一般实现方都会提供扩展功能来支持。
如果你用的是toplink实现包的话, 你就要实现一个DescriptorCustomizer 接口
public class AccountDescriptorCustomizer implements DescriptorCustomizer {
public void customize(ClassDescriptor descriptor) throws Exception {
OneToManyMapping mapping = (OneToManyMapping) descriptor.getMappingForAttributeName("acct2groups");
if (mapping != null) mapping.setIsPrivateOwned(true);
}
}
然后在persistence.xml 中properties节点下加入:
<property name="toplink.descriptor.customizer.包名.Account" value="包名.AccountDescriptorCustomizer"/>
最后Acct2group 中 @ManyToOne 属性cascade将CascadeType.ALL 改成 {CascadeType.REFRESH, CascadeType.MERGE, CascadeType.MERGE}.
如果是hibernate的实现的话, 在@OneToMany
旁边加上: @org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
如果你用的是toplink实现包的话, 你就要实现一个DescriptorCustomizer 接口
public class AccountDescriptorCustomizer implements DescriptorCustomizer {
public void customize(ClassDescriptor descriptor) throws Exception {
OneToManyMapping mapping = (OneToManyMapping) descriptor.getMappingForAttributeName("acct2groups");
if (mapping != null) mapping.setIsPrivateOwned(true);
}
}
然后在persistence.xml 中properties节点下加入:
<property name="toplink.descriptor.customizer.包名.Account" value="包名.AccountDescriptorCustomizer"/>
最后Acct2group 中 @ManyToOne 属性cascade将CascadeType.ALL 改成 {CascadeType.REFRESH, CascadeType.MERGE, CascadeType.MERGE}.
如果是hibernate的实现的话, 在@OneToMany
旁边加上: @org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)