Hi,
I can't seem to map properly 3 objects/tables with cascades, and I get an error when I try to save the top level object. I have already spent more then a few hours trying to figure this one out by myself and I would greatly appreciate some help on the issue.
To make matters simple, I will call the objects A, B and C and the tables a, b, and c.
Here is a simple drawing of how the tables are linked:
http://img2.uploadimages.net/show.php?i ... ernate.jpg
each A can have one or more Bs or Cs, each B can have one or more Cs.
Hibernate version:
2
Mapping documents:
Code:
<class name="A" table="a">
<id name="idA" column="id_a" type="java.lang.Long">
<generator class="identity"/>
</id>
<!-- some other fields that do not matter here -->
<set name="bSet" table="b" cascade="all-delete-orphan" inverse="true" >
<key column="id_a" />
<one-to-many class="B"/>
</set>
<set name="cSet" table="c" cascade="all-delete-orphan" inverse="true" >
<key column="id_a" />
<one-to-many class="C"/>
</set>
</class>
<class name="B" table="b">
<id name="idB" column="id_b" type="java.lang.Long">
<generator class="identity"/>
</id>
<!-- some other fields that do not matter here -->
<many-to-one name="a" class="A" column="id_a" />
<set name="cSet" table="c" cascade="all-delete-orphan" inverse="true">
<key column="id_b" />
<one-to-many class="C" />
</set>
</class>
<class name="C" table="c">
<id name="idC" column="id_c" type="java.lang.Long">
<generator class="identity"/>
</id>
<!-- some other fields that do not matter here -->
<many-to-one name="a" class="A" column="id_a" />
<many-to-one name="b" class="B" column="id_b" />
</class>
Code between sessionFactory.openSession() and session.close():
I only call saveOrUpdateCopy() on an object of type A
Full stack trace of any exception that occurs:
could not insert: [C] - this beeing the package and name of the class C
at net.sf.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:62)
at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at net.sf.hibernate.persister.AbstractEntityPersister.convert(AbstractEntityPersister.java:1331)
...
Cannot insert the value NULL into column 'id_b', table 'c'; column does not allow nulls. INSERT fails.
...
net.sf.hibernate.AssertionFailure: null id in entry (don't flush the Session after an exception occurs)
I could post the generated SQL and complete stack trace, but I don't think that would help more. I am pretty sure I am doing something wrong when mapping the tables.
I am in desperate need of help, cause I've been stuck on this and I just can't seem to find the answer.
Thank you very much.
PS: I really do need cascade="all-delete-orphan" cause it would be hell if I had to save all the objects by myself. But if that is not possible, I would be glad for any solution to this problem.
Thanks again.