[英]Hibernate JPA value removing OneToMany relation
I have two tables where there is a OneToMany, MnatToOne relation. 我有两个表,其中有一个OneToMany,MnatToOne关系。
When I have added instance of AlarmnotifyEmailEntity into alarmnotifyEmailEntityList object and update instance of AlarmnotifyEmailConfEntity, value is save properly into Database. 当我将AlarmnotifyEmailEntity实例添加到alarmnotifyEmailEntityList对象并更新AlarmnotifyEmailConfEntity实例时,值已正确保存到数据库中。
Bu I could not do the same thing when deleting one of the item of alarmnotifyEmailEntityList. 但是,删除alarmnotifyEmailEntityList之一时,我无法做同样的事情。 I am sure that value is removed from alarmnotifyEmailEntityList but it does not reflect this changes into Database 我确定该值已从alarmnotifyEmailEntityList中删除,但不会反映此更改到Database中
@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
I am only invoking following statement to update. 我仅调用以下语句进行更新。
JPAManager.getJPAManagerInstance().update(alarmnotifyemailconf); JPAManager.getJPAManagerInstance()。update(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;
} }
From my experience cascade only applied for same operation. 根据我的经验,级联仅适用于相同的操作。 If we save parent, then children also will saved same case with update. 如果我们保存父母,那么孩子也将保存相同的情况并进行更新。 But I think when you want to remove one of children, we have to remove explicitly using entity manager, and cannot just merging parent and expect will cascade remove to children. 但是我认为,当您要删除一个孩子时,我们必须使用实体管理器显式删除,并且不能仅合并父对象并期望将删除级联到孩子。
I have found out the answer in jpa removing child from collection . 我已经找到了jpa从collection中删除child的答案。 as a result adding orphanRemoval=true solved the problem. 结果,添加orphanRemoval = true解决了该问题。
@Entity(name ="alarmnotify_email_conf") @Table(name = "alarmnotify_email_conf") public class AlarmnotifyEmailConfEntity implements Serializable { @Entity(name =“ alarmnotify_email_conf”)@Table(name =“ alarmnotify_email_conf”)公共类AlarmnotifyEmailConfEntity实现可序列化{
@OneToMany(mappedBy = "alarmnotifyEmailConfRef",cascade=CascadeType.ALL , orphanRemoval=true ) private List alarmnotifyEmailEntityList; @OneToMany(mappedBy =“ alarmnotifyEmailConfRef”,cascade = CascadeType.ALL, orphanRemoval = true )私有列表alarmnotifyEmailEntityList;
}//end of Class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.