本文介绍了休眠JPA值删除OneToMany关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我将AlarmnotifyEmailEntity的实例添加到alarmnotifyEmailEntityList对象并更新AlarmnotifyEmailConfEntity的实例时,值为save正确地进入数据库。



Bu当删除alarmnotifyEmailEntityList的一项时,我无法做同样的事情。
我相信这个值会从alarmnotifyEmailEntityList中移除,但并不会反映到数据库中的这些变化。 =alarmnotify_email_conf)
@Table(name =alarmnotify_email_conf)
public class AlarmnotifyEmailConfEntity implements Serializable {

@OneToMany(mappedBy =alarmnotifyEmailConfRef,cascade = CascadeType.ALL )
私人列表< AlarmnotifyEmailEntity> alarmnotifyEmailEntityList;

$ //
$ b @Entity(name =alarmnotify_email)
@Table(name =alarmnotify_email)
public class AlarmnotifyEmailEntity实现了Serializable {
@JoinColumn(name =alarmnotify_email_conf_ref,referencedColumnName =id)
@ManyToOne
private AlarmnotifyEmailConfEntity alarmnotifyEmailConfRef;
}类的结尾

我只调用以下语句来更新。



JPAManager.getJPAManagerInstance()。update(alarmnotifyemailconf);

  public Object update(对象o){

尝试{
tx.begin();
EntityManager em = getEntityManager();
System.out.println(更新对象:+ o);
o = em.merge(o);
em.close();
tx.commit();
System.out.println(关闭并提交合并操作);
return o;
}
catch(Exception e){
e.printStackTrace();
}
return o;

}

解决方案我已经找到答案在。
作为结果加入 orphanRemoval = true 解决了这个问题。


I have two tables where there is a OneToMany, MnatToOne relation.

When I have added instance of AlarmnotifyEmailEntity into alarmnotifyEmailEntityList object and update instance of AlarmnotifyEmailConfEntity, value is save properly into Database.

Bu I could not do the same thing when deleting one of the item of alarmnotifyEmailEntityList.I am sure that value is removed from alarmnotifyEmailEntityList but it does not reflect this changes into 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);

 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;

}

解决方案

I have found out the answer in jpa removing child from collection.as a result adding orphanRemoval=true solved the problem.

这篇关于休眠JPA值删除OneToMany关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 07:17
查看更多