休眠一对多关系Hbm Xml错误

时间:2019-05-30 18:45:54

标签: java hibernate

我想通过Hibernate中的外键在Definition(DEF)和Details(DET)表之间创建关系。在这种情况下,一个定义具有多个细节。

  • 定义表的 ID 列为 PK
  • 详细信息表的 ID 列为 PK DEFINITION_ID 列为 FK

当我将它们连接到 hbm.xml 内部时,出现诸如以下错误:

错误

  

由于:org.hibernate.MappingException:映射中的重复列   对于实体:com.ykb.hmn.cms.commission.datamodel.ICmsExemptRestrictDet   列:ID(应与insert =“ false” update =“ false”映射)

定义HBM XML

pd.DataFrame(data={'col1': [1, 2], 'A': ['A', 'A']})

详细HBM XML

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.ykb.hmn.cms.commission.datamodel.ICmsExemptRestrictDef"
        table="CMS_EXEMPT_RESTRICT_DEF">
        <tuplizer class="com.ykb.hmn.inf.core.datamodel.IntfEntityTuplizer"
            entity-mode="pojo" />
        <id name="oid" type="java.lang.Long">
            <column name="ID" />
            <generator class="sequence">
                <param name="sequence">SEQ_CMS_EXEMPT_RESTRICT_DEF</param>
            </generator>
        </id>

        .
        .
        .

        <bag name="cmsExemptRestrictDetails" table="CMS_EXEMPT_RESTRICT_DET"
            inverse="true" lazy="true" fetch="select">
            <key>
                <column name="DEFINITION_ID" not-null="true" />
            </key>
            <one-to-many
                class="com.ykb.hmn.cms.commission.datamodel.ICmsExemptRestrictDet" />
        </bag>
    </class>
</hibernate-mapping>

首先-我需要在每个hbm xml中定义多对一和一对多关系吗?

第二个-我是否需要将“详细信息”的列ID重命名为DETAIL_ID?

谢谢。

1 个答案:

答案 0 :(得分:1)

属性映射Definition_id不正确。外键不应该被映射。删除它,然后将多音映射中的列名更改为definition_id:

将hbm xml详细信息更改为:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="com.ykb.hmn.cms.commission.datamodel.ICmsExemptRestrictDet"
        table="CMS_EXEMPT_RESTRICT_DET">
        <tuplizer class="com.ykb.hmn.inf.core.datamodel.IntfEntityTuplizer"
            entity-mode="pojo" />
        <id name="oid" type="java.lang.Long">
            <column name="ID" />
            <generator class="sequence">
                <param name="sequence">SEQ_CMS_EXEMPT_RESTRICT_DET</param>
            </generator>
        </id>

        <many-to-one name="cmsExemptRestrictDefinition" class="com.ykb.hmn.cms.commission.datamodel.ICmsExemptRestrictDef" fetch="select">
            <column name="DEFINITION_ID" not-null="true" />
        </many-to-one>

           </class>
</hibernate-mapping>