我有两个表,其中有一个OneToMany,MnatToOne关系。
当我将AlarmnotifyEmailEntity的实例添加到alarmnotifyEmailEntityList对象并更新AlarmnotifyEmailConfEntity的实例时,值将正确保存到数据库中。
在删除alarmnotifyEmailEntityList项目之一时,我无法做同样的事情。 我确信该值已从alarmnotifyEmailEntityList中删除,但它未将此更改反映到数据库中
@Entity(name ="alarmnotify_email_conf")
@Table(name = "alarmnotify_email_conf")
public class AlarmnotifyEmailConfEntity implements Serializable {
@OneToMany(mappedBy = "alarmnotifyEmailConfRef",cascade=CascadeType.ALL)
private List<AlarmnotifyEmailEntity> alarmnotifyEmailEntityList;
}//end of Class
@Entity (name ="alarmnotify_email")
@Table(name = "alarmnotify_email")
public class AlarmnotifyEmailEntity implements Serializable {
@JoinColumn(name = "alarmnotify_email_conf_ref", referencedColumnName = "id")
@ManyToOne
private AlarmnotifyEmailConfEntity alarmnotifyEmailConfRef;
}end of Class
我只是调用以下语句进行更新。
JPAManager.getJPAManagerInstance()更新(alarmnotifyemailconf);
public Object update(Object o) {
try {
tx.begin();
EntityManager em = getEntityManager();
System.out.println("updating object:" + o);
o = em.merge(o);
em.close();
tx.commit();
System.out.println("closed and commited merge operation");
return o;
}
catch (Exception e) {
e.printStackTrace();
}
return o;
}
答案 0 :(得分:0)
根据我的经验,级联仅适用于相同的操作。如果我们保存父级,那么孩子们也会在更新时保存相同的案例。但是我认为当你想删除其中一个孩子时,我们必须使用实体管理器显式删除,并且不能只是合并父和期望将级联删除给孩子。
答案 1 :(得分:0)
我在jpa removing child from collection找到了答案。 因此,添加 orphanRemoval = true 解决了这个问题。
@Entity(name =“alarmnotify_email_conf”) @Table(name =“alarmnotify_email_conf”) 公共类AlarmnotifyEmailConfEntity实现Serializable {
@OneToMany(mappedBy = “alarmnotifyEmailConfRef”,cascade = CascadeType.ALL, orphanRemoval = true ) private list alarmnotifyEmailEntityList;
}//end of Class