在Hibernate中使用List时放置列表索引

时间:2010-12-17 10:23:17

标签: java database hibernate collections

我正在使用Set但现在由于小部件限制,我需要使用list.a 使用A SET和List的我的映射文件的样本如下。有人可以帮我放置列表索引。我有些困惑。

**Attribute Mapping File using Set**
?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 16, 2010 5:25:09 AM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
    <class name="h.Attribute" table="ATTRIBUTE">
        <id name="AttributeId" type="long">
            <column name="ATTRIBUTEID" />
            <generator class="native" />
        </id>
        <property name="AttributeName" type="java.lang.String">
            <column name="ATTRIBUTENAME" />
        </property>
        <set name="Options" table="ATTRIBUTEOPTION" inverse="false"  cascade="all" lazy="true">
            <key>
                <column name="ATTRIBUTEID" />
            </key>
            <one-to-many class="h.AttributeOption" />
        </set>
    </class>
</hibernate-mapping>



 **Category Mapping File using Set**
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 16, 2010 8:37:02 AM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
    <class name="h.Category" table="CATEGORY">
        <id name="CategoryId" type="long">
            <column name="CATEGORYID" />
            <generator class="native" />
        </id>
        <property name="CategoryName" type="java.lang.String">
            <column name="CATEGORYNAME" />
        </property>

        <many-to-one name="ParentCategory" class="h.Category">
            <column name="PARENT_CATEGORY_ID" />
        </many-to-one>

        <set name="SubCategory" lazy="true" cascade="all-delete-orphan" inverse="true">
            <key>
                <column name="PARENT_CATEGORY_ID" />
            </key>
            <one-to-many class="h.Category" />
        </set>

        <set name="AllAttributes" table="ATTRIBUTE" inverse="false" lazy="true"  cascade="all">
            <key>
                <column name="CATEGORYID" />
            </key>
            <one-to-many class="h.Attribute" />
        </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">
<!-- Generated Dec 17, 2010 2:10:50 AM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
    <class name="h.Category" table="CATEGORY">
        <id name="CategoryId" type="long">
            <column name="CATEGORYID" />
            <generator class="assigned" />
        </id>
        <property name="CategoryName" type="java.lang.String">
            <column name="CATEGORYNAME" />
        </property>
        <many-to-one name="ParentCategory" class="h.Category" fetch="join">
            <column name="PARENTCATEGORY" />
        </many-to-one>
        <list name="SubCategory" inverse="false" table="CATEGORY" lazy="true">
            <key>
                <column name="CATEGORYID" />
            </key>
            <list-index></list-index>
            <one-to-many class="h.Category" />
        </list>
        <list name="AllAttributes" inverse="false" table="ATTRIBUTE" lazy="true"  cascade="all">
            <key>
                <column name="CATEGORYID" />
            </key>
            <list-index></list-index>
            <one-to-many class="h.Attribute" />
        </list>
    </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">
<!-- Generated Dec 17, 2010 2:10:50 AM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
    <class name="h.Attribute" table="ATTRIBUTE">
        <id name="AttributeId" type="long">
            <column name="ATTRIBUTEID" />
            <generator class="assigned" />
        </id>
        <property name="AttributeName" type="java.lang.String">
            <column name="ATTRIBUTENAME" />
        </property>
        <list name="Options" inverse="false" table="ATTRIBUTEOPTION" lazy="true" cascade="all">
            <key>
                <column name="ATTRIBUTEID" />
            </key>
            <list-index></list-index>
            <one-to-many class="h.AttributeOption" />
        </list>
    </class>
</hibernate-mapping>

3 个答案:

答案 0 :(得分:5)

阅读Hibernate Reference: 6.2.3. Indexed collections

例如:

    <list name="whatEver">
        <key column="whatEver_fk"/>
        <index column="idx"/>
        <one-to-many class="WhatEver"/>
    </list>

答案 1 :(得分:3)

Hibernate 3.3 reference说“数组或列表的索引总是整数 ...”,还有“如果你的表没有索引列,那么你仍希望使用 list 作为属性类型,您可以将该属性映射为Hibernate <bag>。“

包的一个例子:

<bag name="options" table="ATTRIBUTEOPTION" order-by="column_name asc|desc" inverse="true" lazy="true" fetch="select">
    <key column="ATTRIBUTEID" />
    <one-to-many class="h.AttributeOption" />
</bag>

答案 2 :(得分:2)

我认为您应该在POJO中为您创建一对多关系的“index”属性

<list name="whatEver">
        <key column="whatEver_fk"/>
        <index column="idx"/>
        <one-to-many class="WhatEver_Class"/>
    </list>

在上面的示例中,“WhatEver_Class”POJO应该具有索引属性,并且它的hbm文件应该具有以下属性标记。

<property name="index" type="long" insert="false" update="false">
            <column name="idx" />
        </property>