I'm definitely quite stuck working with persistent lists. The XML mapping of the entities involved are as follows:
<class name="A"> ... <list name="carichiInCoda" table="codacorsia" access="field"> <cache usage="read-write"/> <key column="corsia" not-null="true"/> <index column="listIndex"/> <many-to-many column="carico" class="C" /> </list>
</class>
<class name="C" abstract="true"> ...
<joined-subclass name="C1" dynamic-update="true"> <key column="id" ></key> ... <list name="scarichi" access="field" cascade="all"> <cache usage="read-write"/> <key column="carico"/> <index column="scaricoIndex"/> <one-to-many class="C2"/> </list>
</joined-sublcass>
<joined-sublcass name="C2"> ... </joined-subclass> <class>
.............. Subclass C1 has a list of subclasses C2.
Summing up, relations between A, C, C1 and C2 are as follow
| A | <>-----> | C | ^ <extends> | ----------------- | | | C1 | <>------->| C2 |
What I'm trying to do is removing a subclass of C (class C1) from the list "carichiInCoda" of an instance of A and then to add another subclass of C (class C2) into the list "carichiInCoda" of a different instance of A. The process runs within a single transaction (handled by an open session in view like pattern implemented in Wicket) and it can be summed up as:
c1 = new C1(); c1.getScarichi().add(c2)
a1.getCarichiInCoda().remove(c1) a2.getCarichiInCoda().add(c2)
where a1, a2 are instances of A, c1 is instance of C1 and c2 is instance of C2
Setting Hibernate to show SQL statements, i can clearly see that it executes the command for the removal of c1 from the persistent list of A ("DELETE FROM codacorsia WHERE .....") but there's no trace of adding c2 into the persistent list of a second instance of a2.
Note that a1, a2 and c1 are instances managed by hibernate, while c2 is created by the new operator and relies on cascading="all" specified in the C1.scarichi mapping. Cascading proves to be working and by all means also the A.carichiInCoda works and is tested in these other transactions:
- adding and removing an element from an A.carichiInCoda collection - removing a c from a1.carichiInCoda and inserting c into a2.carichiInCoda
The only difference that comes up to my mind is that I'm trying to insert a yet-to-be-persisted instance of C into the collection. I've tested that if I commit the transaction before a2.getCarichiInCoda().add(c2) and do this last operation in another one everything works fine (i.e mapping should be correct). Simply flushing doesn't work though.
Is there a reason why doing it in a single transaction shouldn't work? I hadn't problem like this one before...it seems a really odd behaviour.
Thanks for any kind of clarification, I hope i have been clear enough...
|