Here is situation:
A incl B incl C
Code:
class A
{
private Long id_a;
private String name;
private Set<B> bSet;
//def. consturctor
//getters&setters
}
Code:
A.hbm.xml
<hibernate-mapping>
<class name="A" table="a">
<id column="id_a"
name="ID_A"
type="java.lang.Long">
<generator class="sequence">
<param name="sequence">A_seq</param>
</generator>
</id>
<property name="name"
column="NAME"
type="java.lang.String"
length="256"/>
<set name="bSet"
table="b"
lazy="true"
cascade="delete">
<key column="id_a"
not-null="true">
</key>
<one-to-many class="B" />
</set>
</class>
</hibernate-mapping>
Code:
class B
{
private Long id_b;
private Long id_a;
private String name;
private Set<C> cSet;
//def. consturctor
//getters&setters
}
Code:
B.hbm.xml
<hibernate-mapping>
<class name="B" table="b">
<id column="id_b"
name="ID_B"
type="java.lang.Long">
<generator class="sequence">
<param name="sequence">B_seq</param>
</generator>
</id>
<property name="id_a"
column="ID_A"
type="java.lang.String"
length="256"
insert="false"
update="false"/>
<property name="name"
column="NAME"
type="java.lang.String"
length="256"/>
<set name="cSet"
table="c"
lazy="true"
cascade="delete">
<key column="id_b"
not-null="true">
</key>
<one-to-many class="C" />
</set>
</class>
</hibernate-mapping>
Code:
class C
{
private Long id_c;
private Long id_b;
private Clob data;
//def. consturctor
//getters&setters
}
Code:
C.hbm.xml
<hibernate-mapping>
<class name="C" table="c">
<id column="id_c"
name="ID_C"
type="java.lang.Long">
<generator class="sequence">
<param name="sequence">C_seq</param>
</generator>
</id>
<property name="id_b"
column="ID_B"
type="java.lang.String"
length="256"
insert="false"
update="false"/>
<property name="data"
column="DATA"
type="java.lang.Clob"
length="256"/>
</class>
</hibernate-mapping>
client
Code:
//(the row with id_a=1 is already exist)
String stmt = "from A as t where t.id_a=1";
Query query = session.createQuery(stmt);
A a = (A) query.list().get(0);
session.delete(a);
Output
*******************************************************************
>>> org.hibernate.exception.SQLGrammarException: could not initialize a collection: [B.cSet#1]<<<
--> [(1) org.hibernate.exception.SQLStateConverter.convert at: SQLStateConverter.java:92
--> [(2) org.hibernate.exception.JDBCExceptionHelper.convert at: JDBCExceptionHelper.java:66
--> [(3) org.hibernate.loader.Loader.loadCollection at: Loader.java:2173
--> [(4) org.hibernate.loader.collection.CollectionLoader.initialize at: CollectionLoader.java:62
--> [(5) org.hibernate.persister.collection.AbstractCollectionPersister.initialize at: AbstractCollectionPersister.java:627
--> [(6) org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection at: DefaultInitializeCollectionEventListener.java:83
--> [(7) org.hibernate.impl.SessionImpl.initializeCollection at: SessionImpl.java:1863
--> [(8) org.hibernate.collection.AbstractPersistentCollection.initialize at: AbstractPersistentCollection.java:369
--> [(9) org.hibernate.collection.AbstractPersistentCollection.read at: AbstractPersistentCollection.java:111
--> [(10) org.hibernate.collection.PersistentSet.iterator at: PersistentSet.java:186
**********************************************************************
How can I delete ALL references? Not only explicit (from B set) but transitive (from C set) also.
As well as it mustn't be missing the capability to delete B instance with cascade deletion from C.
Hibernate v3.0