Hibernate在保存时重复记录空值(新行作为重复)

时间:2016-11-15 06:23:00

标签: java hibernate hibernate-mapping hibernate-criteria

我在hibernate中保存实体时遇到了以下问题 -

它使用空值重复记录 -

(2661956,2601555, '小克', '小克', 'Thooks'下,NULL,NULL,NULL,NULL,NULL,NULL, 'Y', 'N', 'XYZ' ,TO_DATE('15 -nov-16' , 'DD-MON-RR'),NULL,NULL)

带有副本 -

(2661946,2601555,的 NULL,NULL,NULL 下,NULL,NULL,NULL,NULL,NULL,NULL, 'Y', 'N', 'XYZ',TO_DATE('15 - NOV-16','DD-MON-RR'),null,null) -

我们有以下映射 -

家长 -

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping default-lazy="false">
    <class name="com.sm.persistence.LOADetailBO" table="LOADETAILS">

        <id name="loaDetailsId" type="long">
            <column name="LOA_ID" precision="38" scale="0"/>
            <generator class="com.sm.persistence.dao.seqgen.LOADetailSeqGen">
            </generator>
        </id>
        <many-to-one name="reregistration" class="com.sm.persistence.RerBO" fetch="join">
            <column name="RER_ID" precision="38" scale="0"/>
        </many-to-one>
        <property name="relatedPlanManager" type="string">
            <column name="RELATED_PLAN_MANAGER"/>
        </property>
        <property name="relatedPlanManagerCode" type="string">
            <column name="RELATED_PLAN_MANAGER_CODE"/>
        </property>
        <set name="relatedPlanManagerAddress" inverse="true" cascade="all-delete-orphan">
            <key>
                <column name="LOA_ID" precision="38" scale="0" not-null="false"/>
            </key>
            <one-to-many class="com.sm.persistence.AddressBO" />
        </set>
        <set name="corporateCustomers" inverse="true" cascade="all-delete-orphan">
            <key>
                <column name="LOA_ID" precision="38" scale="0" not-null="false"/>
            </key>
            <one-to-many class="com.sm.persistence.CorporateCustomerBO" />
        </set>
        <set name="privateCustomers" inverse="true" cascade="all-delete-orphan">
            <key>
                <column name="LOA_ID" precision="38" scale="0" not-null="false" />
            </key>            
            <one-to-many class="com.sm.persistence.PrivateCustomerBO"/>
        </set>
        <set name="unwrappedAccount"  inverse="true" cascade="all-delete-orphan">
            <key>
                <column name="LOA_ID" precision="38" scale="0" not-null="false"/>
            </key>
            <one-to-many class="com.sm.persistence.UnwrappedAccountBO" />
        </set>
        <set name="wrappedAccount" inverse="true" cascade="all-delete-orphan">
            <key>
                <column name="LOA_ID" precision="38" scale="0" not-null="false" />
            </key>            
            <one-to-many class="com.sm.persistence.WrappedAccountBO"/>
        </set>       
    </class>
</hibernate-mapping>

儿童 -

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping default-lazy="false">
    <class name="com.sm.persistence.PrivateCustomerBO" table="RER_CUSTOMER_DETAIL">

        <id name="privateCustomerId" type="long">
            <column name="RERE_CUSTOMER_ID" precision="38" scale="0"/>
            <generator class="com.sm.persistence.dao.seqgen.PrivateCustomerSeqGen">
            </generator>
        </id>
        <many-to-one name="loaDetail" class="com.sm.persistence.LOADetailBO" fetch="join">
            <column name="LOA_DETAILS_ID" precision="38" scale="0"/>
        </many-to-one>
        <property name="title" type="string">
            <column name="TITLE"/>
        </property>
        <property name="firstName" type="string">
            <column name="FIRST_NAME"/>
        </property>
        <property name="surname" type="string">
            <column name="SURNAME"/>
        </property>
        <set name="address" inverse="false" cascade="all-delete-orphan">
            <key>
                <column name="PRIVATE_CUSTOMER_ID" precision="38" scale="0" not-null="false"/>
            </key>
            <one-to-many class="com.sm.persistence.AddressBO" />
        </set>
        <property name="primary" type="string">
            <column name="IS_PRIMARY"/>
        </property>
        <property name="corporate" type="string">
            <column name="IS_CORPORATE"/>
        </property>
        <!--<property name="dateofBirth" type="string">
            <column name="DATE_OF_BIRTH"/>
        </property>-->     
        <property name="nationalInsurance" type="string">
            <column name="NINO"/>
        </property>
    </class>
</hibernate-mapping>

我检查了保存前后的父对象,但找不到任何填充了空值的对象。但是,当我使用条件api获取对象层次结构时,它返回带有null的重复记录。

请指导。

1 个答案:

答案 0 :(得分:0)

快速回答:

  1. PrivateCustomerBO是LOADetailBO的孩子。所以当你的java代码 保存一个将写入所有表的PrivateCustomerBO对象 针对这两个对象指定:RER_CUSTOMER_DETAIL = primary privateCustomerBO的表,LOADETAILS =主表 LOADetailBO, 以及这两个对象引用的所有表格 多对一或一对多映射 。正确的行为
  2. 扩展上面的粗体位,LOADetailBO被引用 PrivateCustomerBO via:
    <many-to-one name="loaDetail" class="com.sm.persistence.LOADetailBO" fetch="join"> <column name="LOA_DETAILS_ID" precision="38" scale="0"/> </many-to-one>
    这意味着LOADETAILS另外被引用为相关的 表,不只是父和第二行(在这种情况下) 有一些数据缺失(NULL)因为你的程序没有 为引用的实体填充它。不正当的行为。
  3. 修复:删除上面显示的多对一映射(它是多余的,由父级覆盖)。