We have the following problem: our child objects do not get removed from the database.
Using Hibernate we persist objects of a class called "Aanvraag", this
class contains a list of other objects of the type "BijzondereBepaling".
When we remove "BijzondereBepaling" objects from this list they do not get
deleted in our database (Sybase). If we add elements to the list they do get added
in our database.
The relation between "Aanvraag" and "BijzondereBepaling" is "one to many"
We tried to follow the instructions from the reference manual paragraph 21.1 and
we've tried all suggestions mentioned in the FAQ and this forum but none of
the suggestions works.
Some things we played around with:
- not-null settings
- inverse settings
- "all-delete-orphan" cascade settings
We do not explicitly delete the children objects in our code, but use the following code snippet:
Code:
HibernateUtil.beginTransaction();
HibernateUtil.getSession().saveOrUpdate(aanvraagObj);
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
In the reference guide it is suggested to explictly delete the children, but this is something
we want to avoid (we do not want to know if the user removed a child from the parent - it is all
abstracted away from us).
We are Hibernate newbies and a bit in the dark, so can anyone help us with this problem?
-------------- AANVRAAG CLASS SNIPPET ------------
Code:
public class Aanvraag extends AbstractBusinessObject implements Values {
private List bijzondereBepalinglijst;
.. etc ..
-------------- AANVRAAG HIBERNATE MAPPING FILE ------------
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="nl.snsbank.fom">
<class name="Aanvraag" table="AANVRAAG" >
<!-- properties van AbstractBusinessObject -->
<id name="objectID" type="long">
<column name="id" />
<generator class="native" />
</id>
<bag name="bijzondereBepalinglijst"
cascade="all-delete-orphan" inverse="true">
<key column="aanvraag_id"
foreign-key="fk_fbb_a" />
<one-to-many class="bijzondereBepaling"/>
</bag>
..etc.
-------------- BIJZONDEREBEPALING HIBERNATE MAPPING FILE ------------
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="nl.snsbank.fom">
<class name="BijzondereBepaling" table="BIJZONDEREBEPALING" >
<!-- properties van AbstractBusinessObject -->
<id name="objectID" type="long">
<column name="id" />
<generator class="native" />
</id>
<many-to-one name="aanvraag" column="aanvraag_id" not-null="true"/>
<!-- properties van BijzondereBepaling -->
<property name="tekst" type="text" />
..etc..
-------------- BIJZONDEREBEPALING TABLE CREATION SCRIPT ------------
Code:
create table dbo.BIJZONDEREBEPALING (
id numeric(19,0)
identity not null,
aanvraag_id numeric(19,0) not null,
GO
PRINT 'PRIMARY KEY CONSTRAINT(S)'
GO
ALTER TABLE dbo.BIJZONDEREBEPALING ADD primary key clustered (id)
GO
PRINT 'FOREIGN KEY CONSTRAINT(S)'
GO
ALTER TABLE dbo.BIJZONDEREBEPALING ADD constraint fk_fbb_a
foreign key (aanvraag_id)
references dbo.AANVRAAG (id)
GO