我将JPA用于两个表之间的经典关系(1:N):
RefCollectivite(1)==>(N)ColPlanCompteM61
我希望在删除RefCollectivite实体时进行级联删除。 除了刚刚创建RefCollectivite实体(当我创建1 RefCollectivite时,我还创建232 ColPlanCompteM61)时,级联删除效果很好。 删除RefCollectivite时,未检测到ColPlanCompteM61实体,并且由于违反了外键而导致删除失败。
我的持久RefCollectivite代码:
public void creerRefCollectivite(RefCollectivite collectivite) {
try {
getEntityManager().persist(collectivite);
getEntityManager().flush();
} catch (Exception e) {
}
}
坚持ColPlanCompteM61:
public void initialiserCollectivite(RefCollectivite collectivite) {
List<RefPlanCompteM61> listeRefPlanCompteM61 = getRefPlanCompteM61Facade().rechercherListeRefPlanCompteM61();
for (RefPlanCompteM61 ligne : listeRefPlanCompteM61) {
ColPlanCompteM61 colPlanCompteM61 = new ColPlanCompteM61(ligne, collectivite);
try {
getEntityManager().persist(colPlanCompteM61);
} catch (Exception e) {
}
}
}
我要删除的代码:
public void supprimerRefCollectivite(RefCollectivite collectivite) {
try {
getEntityManager().remove(rechercherRefCollectivite(collectivite));
} catch (NoResultException e) {
}
}
public RefCollectivite rechercherRefCollectivite(RefCollectivite collectivite) {
Query jQuery = getEntityManager().createQuery("select x from RefCollectivite x where x.idPk = :clePrimaire");
jQuery.setParameter("clePrimaire", collectivite.getIdPk());
try {
return (RefCollectivite) jQuery.getSingleResult();
} catch (NoResultException e) {
return null;
}
}
实体:RefCollectivite
@Entity
@Table(name = "RefCollectivite")
public class RefCollectivite implements Serializable {
@OneToMany(cascade = CascadeType.REMOVE, mappedBy="refCollectivite")
private List<ColPlanCompteM61> listePlanCompteM61;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int idPk;
实体:ColPlanCompteM61:
@Entity
@Table(name = "ColPlanCompteM61")
public class ColPlanCompteM61 implements Serializable{
@ManyToOne
@JoinColumn(name = "REFCOLLECTIVITEFK", referencedColumnName = "IDPK", insertable = false, updatable = false)
private RefCollectivite refCollectivite;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int idPk;
private int refCollectiviteFk;
private int refPlanCompteM61Fk;
private Double valeur01;
private Double valeur02;
private Double valeur03;
private Double valeur04;
private Double valeur05;
答案 0 :(得分:0)
要级联删除“ more aggressive remove cascading mode”中的实体,您可以在orphanRemoval=true
的关系注释中添加RefCollectivite
属性。而且,请添加一些有关RefCollectivite及其JPA设计(关系)的信息。