我无法使用Hibernate JPA实现让cascase remove在Java EE容器托管应用程序中工作。帮助将不胜感激。 我有一个实体'公司',与其他实体有四个OneToMany关系。在测试应用程序时(使用Arquillian框架),我无法使级联删除工作;公司实体被删除,删除后子实体仍然存在。 我在这里读到:JPA and Hibernate Cascade DELETE OneToMany does not work这可能是一个测试问题,因此我在这里阅读了关于这个主题的每篇文章,并尝试了所有可能性,我现在认为它可能与Arquillian有关测试框架和(未)在测试部署中加载的类。
我的公司实体看起来像这样:
public class CompanyEntity implements Serializable {
private static final long serialVersionUID = 2L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
private String name;
@NotNull
private String companyCode;
@OneToMany(targetEntity = ContactInfoEntity.class, cascade = CascadeType.REMOVE, orphanRemoval = true)
private List<ContactInfoEntity> contactInfo;
@OneToMany(targetEntity = AddressEntity.class,cascade = CascadeType.REMOVE, orphanRemoval = true)
private List<AddressEntity> addresses;
@OneToMany(targetEntity = ConnectionInfoEntity.class,cascade = CascadeType.REMOVE, orphanRemoval = true)
private List<ConnectionInfoEntity> connectionInformation;
@OneToMany(targetEntity = PreferenceEntity.class,cascade = CascadeType.REMOVE, orphanRemoval = true)
private List<CompanyPreferenceEntity> preferences;
}
我的实体都以这种方式定义:
public class AddressEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
@Size(min = 2)
private String country;
@NotNull
@Size(min = 2)
private String city;
private String zipCode;
private String street;
private boolean isPrimaryAddress;
}
删除功能执行以下操作:
提前致谢!
答案 0 :(得分:0)
您需要提供@JoinColumn
注释以指示关系的所有者:
@OneToMany(targetEntity = ContactInfoEntity.class, cascade =
CascadeType.REMOVE, orphanRemoval = true)
@JoinColumn(name="company_id")
private List<ContactInfoEntity> contactInfo;
有关此类映射的详细信息,请访问:https://en.wikibooks.org/wiki/Java_Persistence/OneToMany#Unidirectional_OneToMany,_No_Inverse_ManyToOne,_No_Join_Table_(JPA_2.x_ONLY)
答案 1 :(得分:0)
通过以错误的方式实施我的测试,结果证明这是我自己的错。让我解释一下我的测试内容: