Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:
3.2.4 SP1
Hello all!
Sorry for my english at first.
I have a legacy DB schema, and I'm not allowed to change anything.
The scheme looks like that:
Code:
|--TableA----------|1 |--TableB----------| 1..*|--TableC----------| |--TableD----------|
|TableA_ID (PK) |<--| 1..*|TableB_ID (PK) |<----------|TableB_ID (PK,FK) | |--->|TableD_ID (PK,FK) |
|------------------| |--------|TableA_ID (FK) | 1 |TableD_ID (PK,FK) |----| 1|Bemerkung |
|LFDNR | |LFDNR |1..* |------------------|
|------------------| |------------------|
I wrote some mapping-files by hand for each table:
Mapping documents:Mapping of Class A:
Code:
<hibernate-mapping>
<class name="ClassA" table="TableA">
<id name="id" column="TableA_ID" />
<bag name="bTables" cascade="all" table="TableB" >
<key column="TableA_ID"/>
<one-to-many class="ClassB"/>
</bag>
</class>
</hibernate-mapping>
Mapping of Class B:
Code:
<hibernate-mapping>
<class name="ClassB" table="TableB">
<id name="id" column="TableB_ID""/>
<bag name="cTables" order-by="lfdNr" cascade="all" fetch="join">
<key column="TableB_ID" unique="true"/>
<one-to-many class="ClassC" />
</bag>
<many-to-one name="classA" column="TableA_ID" cascade="all" class="ClassA"/>
<property name="lfdNr">
<column name="LFDNR" sql-type="Number(3)"/>
</property>
</class>
</hibernate-mapping>
Mapping of Class C:
Code:
<hibernate-mapping>
<class name="ClassC" table="TableC" >
<composite-id >
<key-many-to-one
name="classD"
column="TableD_ID"
class="ClassD"/>
<key-many-to-one
name="classB"
column="TableB_ID"
class="ClassB" />
</composite-id>
<many-to-one name="classD" not-null="true" insert="false" update="false" cascade="all," fetch="join" column="TableD_ID" />
<many-to-one name="classB" not-null="true" insert="false" update="false" cascade="all" fetch="join" column="TableB_ID" />
<property name="lfdNr" column="LFDNR" type="int"/>
</class>
</hibernate-mapping>
Mapping of Class D:
Code:
<hibernate-mapping>
<class name="ClassD" table="TableD">
<id name="id">
<column name="TableD_ID"/>
<generator class="guid"></generator>
</id>
<bag name="cTables" cascade="all">
<key column="TableD_ID"/>
<one-to-many class="ClassC"/>
</bag>
<property name="bemerkung">
<column name="BEMERKUNG" sql-type="VARCHAR2(30)"/>
</property>
</class>
</hibernate-mapping>
All this stuff works, and no exceptions are thrown, but the problem is the creating of a new object of ClassC.
A new ClassC-object means, that a new ClassD-object should be created and the ClassB-object can be one of the existing objects.
So I create a new ClassC-object, whereas a ClassD-object is created automatically by the constructor and the ClassB Attribute is assigned to an existing ClassB-object.
After that I say to the session, that there are new objects:
Code:
Session session = HibernateUtil.getSession();
session.saveOrUpdate(classC.getClassD);
session.saveOrUpdate(classC);
session.saveOrUpdate(classA);
session.close();
But I don't want to make a transaction at this time, so that the new objects are not persisted yet.
Later on when there should be the transaction, I want to persist the whole ClassA-object and all it's connections, and now I thougt Hibernate can detect all the new objects in the hierarchy and persist also the new ClassC-object and ClassD -object.
But this doesn't happen!! Nothing of the new children(ClassC) of the children(ClassB) from ClassA are persistet?
What should be changed in the mapping files?
thanks for helping.