从集合中删除元素
我有 2 个课程 Tema(作业)和 Disciplina(课程),其中课程有一组作业。 在 Hibernate 中,我已将其映射为一对多关联,如下所示:
<class name="model.Disciplina" table="devgar_scoala.discipline" >
<id name="id" >
<generator class="increment"/>
</id>
<set name="listaTeme" table="devgar_scoala.teme">
<key column="Discipline_id" not-null="true" ></key>
<one-to-many class="model.Tema" ></one-to-many>
</set>
</class>
<class name="model.Tema" table="devgar_scoala.teme" >
<id name="id">
<generator class="increment" />
</id>
<property name="titlu" type="string" />
<property name="cerinta" type="binary">
<column name="cerinta" sql-type="blob" />
</property>
</class>
问题是它将添加(在表“Teme”中插入行),但它不会删除任何行,并且我不会抛出任何异常。
我使用 merge() 方法。
I have 2 classes Tema(Homework) and Disciplina (course), where a Course has a Set of homeworks.
In Hibernate i have mapped this to a one-to-many associations like this:
<class name="model.Disciplina" table="devgar_scoala.discipline" >
<id name="id" >
<generator class="increment"/>
</id>
<set name="listaTeme" table="devgar_scoala.teme">
<key column="Discipline_id" not-null="true" ></key>
<one-to-many class="model.Tema" ></one-to-many>
</set>
</class>
<class name="model.Tema" table="devgar_scoala.teme" >
<id name="id">
<generator class="increment" />
</id>
<property name="titlu" type="string" />
<property name="cerinta" type="binary">
<column name="cerinta" sql-type="blob" />
</property>
</class>
The problem is that it will add (insert rows in the table 'Teme') but it won't delete any rows and i get no exceptions thrown.
Im using the merge() method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然您的问题不清楚(如何保存和删除?),但我建议您需要设置
cascade
:作为旁注 - 避免使用您的母语名称。
Although your question is unclear (how do you save and delete?), I'd suggest you need to set
cascade
:As a sidenote - avoid names in your native language.
根据您的描述,我了解
Tema
不能没有其Disciplina
而存在:如果您从集合中删除Tema
,您希望它被删除。要告诉 Hibernate 执行此操作,您必须使用cascade="all-delete-orphan"
。请参阅在线文档。
According to your description, I understand that a
Tema
cannot exist without itsDisciplina
: if you remove aTema
from the collection, you want it to be deleted. To tell Hibernate to do this, you must usecascade="all-delete-orphan"
.Refer to the online documentation.