views:

46

answers:

2

Hi. 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.

A: 

Although your question is unclear (how do you save and delete?), I'd suggest you need to set cascade:

<set cascade="all-delete-orphan">

As a sidenote - avoid names in your native language.

Bozho
thanks. it works!
Blitzkr1eg
A: 

According to your description, I understand that a Tema cannot exist without its Disciplina: if you remove a Tema from the collection, you want it to be deleted. To tell Hibernate to do this, you must use cascade="all-delete-orphan".

<set name="listaTeme" table="devgar_scoala.teme" cascade="all-delete-orphan">
    <key column="Discipline_id" not-null="true" ></key>
    <one-to-many class="model.Tema" ></one-to-many>
</set>

Refer to the online documentation.

Pascal Thivent