Hibernate加载不加载整个树

时间:2010-12-17 07:56:40

标签: java database hibernate tree

我试图让hibernate加载我的整个树而没有成功。

我有这样的架构 类别可以有许多属性,属性可以有很多选项

Category
    --->Attribute1
        --->AttributeOption1
        --->AttributeOption3
    --->Attribute2
        --->AttributeOption1
        --->AttributeOption1

但是当我使用hibernate时,它没有加载整个树: 我的映射文件是:

Attribute Mapping FIle:
<?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="com.BiddingSystem.Models.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="com.BiddingSystem.Models.AttributeOption" />
        </set>
    </class>
</hibernate-mapping>

Attribute Option Mapping File:
<?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="com.BiddingSystem.Models.AttributeOption" table="ATTRIBUTEOPTION">
        <id name="AttributeOptionId" type="long">
            <column name="ATTRIBUTEOPTIONID" />
            <generator class="native" />
        </id>
        <property name="Option" type="java.lang.String">
            <column name="OPTION" />
        </property>
        <property name="SQLValue" type="java.lang.String">
            <column name="SQLVALUE" />
        </property>
    </class>
</hibernate-mapping>

Category Mapping File:
<?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="com.BiddingSystem.Models.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="com.BiddingSystem.Models.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="com.BiddingSystem.Models.Category" />
        </set>

        <set name="AllAttributes" table="ATTRIBUTE"  inverse="false" lazy="true"  cascade="all">
            <key>
                <column name="CATEGORYID" />
            </key>
            <one-to-many class="com.BiddingSystem.Models.Attribute" />
        </set>

    </class>
</hibernate-mapping>

加载父类别的函数是:

public List <Category> getCategory(long CategoryId) 
    {
        Session session = gileadHibernateUtil.getSessionFactory().openSession();
        List <Category> AllCategory= new LinkedList<Category> ();
        String SQL="from Category where parent_category_id is NULL";
        Query query = session.createQuery(SQL);
        return query.list();
    }

2 个答案:

答案 0 :(得分:1)

让你所有的关联懒惰=假;默认情况下,lazy = true

答案 1 :(得分:1)

我相信还有一个错误:

String SQL="from Category where parent_category_id is NULL";
Query query = session.createQuery(SQL);

org.hibernate.Session.createQuery(String queryString)需要HQL查询字符串,但不需要SQL查询! from Category where parent_category_id is NULL似乎是一个SQL查询,因为parent_category_id是列名而不是属性/字段名。