2011-10-07 201 views
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()) { 
      } 
     } 
    } 
} 

閱讀課程後,我可以遍歷它的模塊,但持續的一系列問題可以不會被訪問。 (size ='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> 

我想答案是簡單的,但如果任何人都可以幫助我,我很友善。

感謝名單

回答

0

默認休眠不會加載集合時列表或設置是used.If你想,加入爲lazy =「假」關閉懶惰的標誌。然後,無論何時加載模塊,所有的問題都會被加載。

<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());關閉會話之前。

瞭解更多關於休眠集合映射的信息here。或者看一個例子here