0

Hibernate を使用してデータベースから読み込んでいます。私はあまり詳しくないので、答えは簡単かもしれません:

次の簡単なコードを使用します。

public static void main(String[] args) {
    CourseHelper ch = new CourseHelper();
    Course c = ch.readCourse("fkaea");
    for (Module m : c.getModules()) {
        for (Question q : m.getQuestions()) {
            for (Answer a : q.getAnswers()) {
            }
        }
    }
}

コースを読んだ後、そのモジュールを反復処理できますが、永続的な質問セットにはアクセスできません。(サイズ = '0') 質問はありますが。

休止状態の構成ファイルは次のとおりです。

最初のモジュール マッピング:

<hibernate-mapping>
    <class name="hibernate.dao.Module" table="module" catalog="questionnair">
        <composite-id name="id" class="hibernate.dao.ModuleId">
            <key-property name="idmodule" type="int">
                <column name="idmodule" />
            </key-property>
            <key-property name="idcourse" type="string">
               <column name="idcourse" length="12" />
            </key-property>
        </composite-id>
        <many-to-one name="course" class="hibernate.dao.Course" update="false" insert="false" fetch="select">
            <column name="idcourse" length="12" not-null="true" />
        </many-to-one>
        <property name="omschrijving" type="string">
            <column name="omschrijving" length="45" />
        </property>
        <set name="questions" inverse="true">
            <key>
                <column name="idcourse" />
                <column name="idmodule" length="12" />
            </key>
            <one-to-many class="hibernate.dao.Question" />
        </set>
    </class>
</hibernate-mapping>

質問のマッピング:

<hibernate-mapping>
    <class name="hibernate.dao.Question" table="question" catalog="questionnair">
        <id name="idvraag" type="java.lang.Integer">
            <column name="idvraag" />
            <generator class="identity" />
        </id>
        <many-to-one name="module" class="hibernate.dao.Module" fetch="select">
            <column name="idcourse" />
            <column name="idmodule" length="12" />
        </many-to-one>
        <property name="vraag" type="string">
            <column name="vraag" length="245" />
        </property>
        <set name="answers" inverse="true">
            <key>
                <column name="idvraag" not-null="true" />
            </key>
            <one-to-many class="hibernate.dao.Answer" />
        </set>
        <set name="testquestionses" inverse="true">
            <key>
                <column name="idquestion" not-null="true" />
            </key>
            <one-to-many class="hibernate.dao.Testquestions" />
        </set>
    </class>
</hibernate-mapping>

答えは簡単だと思いますが、誰かが私を助けてくれるなら、私は感謝しています.

ありがとう

4

1 に答える 1

0

デフォルトでは、リストまたはセットが使用されている場合、hibernate はコレクションをロードしません。必要に応じて、lazy="false"を追加して遅延フラグをオフにします。モジュールが読み込まれるたびに、すべての質問が読み込まれます。

   <set name="questions" inverse="true" lazy="false">
        <key>
            <column name="idcourse" />
            <column name="idmodule" length="12" />
        </key>
        <one-to-many class="hibernate.dao.Question" />
    </set>

マッピングを変更せず、この特定のシナリオでコレクションをロードしたい場合は、Hibernate.initialize(module.getQuestions());を使用できます。セッションを閉じる前に。

hibernate でのコレクション マッピングの詳細については、こちらをご覧ください。または、こちらの例をご覧ください。

于 2011-10-09T11:00:03.233 に答える