Hibernate 找不到持久集
我正在使用 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 配置文件如下:
首先是模块映射:
<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>
我想答案很简单,但如果有人可以帮助我,我将不胜感激。
谢谢
I'm reading from a database using Hibernate. I am not too familiar with it, so maybe the answer is simple:
I use the following simple code:
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()) {
}
}
}
}
After reading the Course I can iterate through it's modules but the persistent set of questions can't be accessed. (size = '0') Although there are questions.
The hibernate config files are as follows:
First the Module mapping:
<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>
The Question 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>
I suppose the answer will be simple but if anyone can help me out, I'm gratefull.
Thanx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,当使用列表或集合时,hibernate 不会加载集合。如果需要,可以通过添加 lazy="false" 来关闭惰性标志。然后,每当加载模块时,所有问题都会被加载。
如果您不想更改映射并希望在这种特定情况下加载集合,您可以在之前使用 Hibernate.initialize(module.getQuestions());关闭会话。
在此处了解有关 hibernate 中集合映射的更多信息。或者查看此处的示例。
By default hibernate wont load the collections when the list or set is used.If you want, turn off the lazy flag by adding lazy="false". Then all the questions will be loaded whenever Module is loaded.
If you don't want to change you mappings and want to load a collection in this specific scenario you can use Hibernate.initialize(module.getQuestions()); before you close the session.
Read more about collection mappings in hibernate here. Or Have a look at an example here.