初始SessionFactory创建失败..org.hibernate.MappingException:重复列

时间:2014-05-24 08:25:32

标签: java hibernate

我在调用main方法时遇到异常。 我在这里发布映射文件。 我认为这是由于

 <list-index>
   <column name="bill_no" />
  </list-index>

这个

应该有什么价值
    <hibernate-mapping>
    <class name="iland.hbm.BillDetails" table="bill_details" catalog="retail_shop">
        <id name="billNo" type="java.lang.Long">
            <column name="bill_no" />
            <generator class="identity"></generator>
        </id>
        <many-to-one name="customerDetails" class="iland.hbm.CustomerDetails" fetch="join">
            <column name="customer_id" not-null="true" />
        </many-to-one>
        <property name="subTotal" type="java.lang.Float">
            <column name="sub_total" precision="10" />
        </property>

        <list name="billProductDetailses" table="bill_product_details" inverse="true" lazy="false" fetch="join">
            <key>
                <column name="bill_no" not-null="true" />
            </key>
            <list-index>
                <column name="bill_no" />
            </list-index>
            <one-to-many class="iland.hbm.BillProductDetails" />
        </list>
    </class>
</hibernate-mapping>

错误:

Initial SessionFactory creation failed.org.hibernate.MappingException: Repeated column in mapping for collection: iland.hbm.BillDetails.billProductDetailses column: bill_no
Exception in thread "main" java.lang.ExceptionInInitializerError
    at iland.database.HibernateUtil.<clinit>(HibernateUtil.java:32)
    at iland.bill.BillDAO.fetchAll(BillDAO.java:94)
        Caused by: org.hibernate.MappingException: Repeated column in mapping for collection: iland.hbm.BillDetails.billProductDetailses column: bill_no
    at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:341)
    at org.hibernate.mapping.Collection.checkColumnDuplication(Collection.java:354)

如何解决上述错误。

1 个答案:

答案 0 :(得分:0)

实际上,您要在实体中映射两次相同的列。

    <list name="billProductDetailses" table="bill_product_details" inverse="true" lazy="false" fetch="join">
        <key>
            <column name="bill_no" not-null="true" />
        </key>
        <list-index>
            <column name="bill_no" />
        </list-index>
        <one-to-many class="iland.hbm.BillProductDetails" />
    </list>

当你在一个实体中映射两次相同的列(bill_product_details.bill_no)时(假设对象的属性A和B引用同一列),Hibernate会产生该异常,因为它不知道何时更新/插入db上的数据(当A被更改或B被更改时?如果A和B不同?),所以你必须告诉它哪一个是“主”,比如说。

另外,我要指出我不理解你的映射。 “key”应该映射到具有外键约束的列,而索引应该映射列,指示对象在列表/数组中的位置,所以我真的不明白为什么它们都引用同一列。这样,数组中对象的位置将等于引用条目的id。不是吗?

相关问题