Hello,
I'm trying to map the following Object relations:
AssemblyType - contains a Collection of Assemblies
Assembly - contains a map with the key bieng of type PiecePart and the value being an Integer representing the quantity of said part.
I wish to perform a "save" on the AssemblyType and have it cascade through to the PiecePart level. Relavent bits of my mapping files:
AssemblyType.hbm.xml:
-----------------------------
<set name="assemblyList" inverse="true" lazy="false"
cascade="persist,merge,save-update,delete-orphan">
<key column="assemblyTypeID"/>
<one-to-many class="Assembly"/>
</set>
Assembly.hbm.xml:
-----------------------
<many-to-one name="AssemblyType" column="assemblyTypeID" class="AssemblyType"/>
<map name="piecePartMap" table="AssemblyPieceParts" lazy="false"
cascade="persist,merge,save-update">
<key column="assemblyID"/>
<map-key-many-to-many class="PiecePart" column="partID"/>
<element type="int" column="quantity"/>
</map>
PiecePart.hbm.xml: (nothing special here)
----------------------
<property name="partNumber" type="string" unique="true" not-null="true"/>
<property name="description" type="string" not-null="true"/>
These mapping files build the tables I wish and work *if* the PieceParts are already defined, but generate a TransientObjectException if a PiecePart is not yet defined. I was able to hack a "solution" to force a save of the PiecePart by modifying Assembly.hbm.xml to include:
<set name="pieceParts" table="PiecePartHack" lazy="false"
cascade="persist,merge,save-update">
<meta attribute="description">
A (hopefully-temporary) hack to force hibernate to persist parts
before creating the map table "piecePartMap"
</meta>
<key column="piecePartID"/>
<many-to-many class="PiecePart"/>
</set>
This cobbled hack forces me to keep an extra Collection on the Java side and create an extra table on the db side -- not ideal.
I'm far from an expert (which is perhaps obvious), but have read the forum, The Book, and anything else I can get my hands on -- so if the solution should be obvious, please blame ignorance rather than laziness.
thanx,
-don.
|