Hibernate双向父/子问题 [英] Hibernate bidirectional parent/child problem

查看:129
本文介绍了Hibernate双向父/子问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用hibernate 3实现双向父/子关系时遇到问题。在这种情况下,父类是ReportCriteria类。这个孩子属于PkVisit班。我已经粘贴了我的hibernate配置文件以及下面的底层java对象。

I'm having a problem implementing a bi-directional parent/child relationship using hibernate 3. The parent, in this case is of the class ReportCriteria. The child is of class PkVisit. I've pasted my hibernate configuration files as well as the underlying java objects below.

ReportCriteria配置:

ReportCriteria configuration:

<hibernate-mapping package="org.fstrf.masterpk.domain">
<class name="ReportCriteriaBean" table="masterPkReportCriteria">

    <id name="id" column="id">
        <generator class="org.hibernate.id.IncrementGenerator" />
    </id>

    <bag name="pkVisits" table="masterPkWeeks" cascade="all-delete-orphan" inverse="true">
        <key column="runId"/>
        <one-to-many class="PkVisit"/>
    </bag>
</class>
</hibernate-mapping>

ReportCriteria bean:

ReportCriteria bean:

public class ReportCriteriaBean {

private Integer id;

private List<PkVisit> pkVisits =  LazyList.decorate(new ArrayList(), FactoryUtils.instantiateFactory(PkVisit.class));

public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}

public List<PkVisit> getPkVisits() {
    return pkVisits;
}

public void setPkVisits(List<PkVisit> pkVisits) {
    this.pkVisits = pkVisits;
}

}

PkVisit配置:

PkVisit Configuration:

<hibernate-mapping package="org.fstrf.masterpk.domain">
<class name="PkVisit" table="masterPkWeeks">
    <id name="id" column="id">
        <generator class="org.hibernate.id.IncrementGenerator" />
    </id>

    <many-to-one name="reportCriteriaBean" class="ReportCriteriaBean" column="runid" not-null="true" />

    <property name="week" column="week" />

</class>
</hibernate-mapping>

PkVisit Bean:

PkVisit Bean:

public class PkVisit {
private Integer id;

private ReportCriteriaBean reportCriteriaBean;

private Integer week;

public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}
public ReportCriteriaBean getReportCriteriaBean() {
    return reportCriteriaBean;
}
public void setReportCriteriaBean(ReportCriteriaBean reportCriteriaBean) {
    this.reportCriteriaBean = reportCriteriaBean;
}
public Integer getWeek() {
    return week;
}
public void setWeek(Integer week) {
    this.week = week;
}
}

当我尝试保存实例时出现问题ReportCriteria,由于级联,也应该保存任何子PkVisits。但是,当使用

The problem occurs when I try to save an instance of ReportCriteria, which, due to the cascade should also save any child PkVisits as well. However, when the save is called using

hibernateTemplate.saveOrUpdate(reportCriteria);

生成以下错误:

org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: org.fstrf.masterpk.domain.PkVisit.reportCriteriaBean; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value: org.fstrf.masterpk.domain.PkVisit.reportCriteriaBean

当我保存不包含PkVisits的报告条件时,一切都按预期工作,但只要任何元素都在ReportCriteria bean的pkVisits列表中,就会发生错误。

When I save a report criteria that contains no PkVisits then everything works as I would expect, but as soon as any elements are in the pkVisits list of the ReportCriteria bean the errors occurs.

解决方案编辑:

我的问题是我从未明确设置子项中的父项(ReportCriteriaBean)(PkVisits) 。我通过以下方式编辑我的PkVisits setter来解决这个问题:

My problem was that I was never explicitly setting the parent (ReportCriteriaBean) in the children (PkVisits). I remedied the problem by editing my PkVisits setter in the following way:

public void setPkVisits(List<PkVisit> pkVisits) {
    this.pkVisits = pkVisits;
    for(PkVisit visit : pkVisits){
        visit.setReportCriteriaBean(this);
    }
}


推荐答案

它看来你没有正确地在java中创建双向链接。我建议在ReportCriteriaBean上创建一个add方法;产生影响:

It appears that you are not creating the bidirectional link in java properly. I'd recommend creating an add method on ReportCriteriaBean; something to the effect of:

public boolean add(PkVisit pkVisit) {
   boolean added = false;
   added = getPkVisits().add(pkVisit);
   if (added) {
       pkVisit.setReportCriteriaBean(this);
   }
   return added;
}

该错误表示如果其ReportCriteriaBean为空,则无法保存PkVisit。我认为上面的代码是你缺失的链接。如果你走这条路线,你只需要在保持报告标准之前将PkVisit添加到ReportCriteriaBean,一切都应该没问题。

The error indicates that you cannot save a PkVisit if its ReportCriteriaBean is null. The above code, i think, is your missing link. If you go this route, you just add the PkVisit to the ReportCriteriaBean before persisting the report criteria and all should be well.

另外,这里有关于hibernate文档的链接这个主题,部分21.2

Also, here's a link to the hibernate documentation on this subject, section 21.2

这篇关于Hibernate双向父/子问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆