Hi,
I have a one-to-many relationship defined as follows.
1. FirstElement.java is Parent Class and holds a set of SecondElement.java in a HastSet
2. In FirstElement.java there is a property called "secondElementKey" which is used to load the HashSet
Both FirstElement.java and SecondElement.java classes are binded using column "second_element_key"
The Question i have is, is there any way Hibernate can generate a new value for "secondElementKey" if it's un-initialzed at the time Database call save() function is called.
Right now if i don't set a value for "secondElementKey" then no Key is generated and NULL is saved in both "first_element" and "second_element" tables for the "second_element_key" column.
Notes : I think it will generate a Unique Key if SecondElement.java has reference to FirstElement.java and then i have a reverse "<many-to-one>" mapping to FirstElement.java from SecondElement.java
But then the current situation is, SecondElement.java is used by more than one class, and not just by FirstElement.java. So can't add a reverse mapping here.
Any Suggestions on how i can generate a Unique Key Each time automatically ? I was planning to use table "last_second_element" defined by (Class 3 below) to generate the next available Key.
Here are the Mapping File definations.
Hibernate version: 3.0.5
Class 1 : Definition
--------------------
<class name="FirstElement" table="first_element"
dynamic-update="true"
dynamic-insert="true"
select-before-update="true" >
<id name="serialNum" column="serial_num" unsaved-value="undefined" >
<generator class="native"/>
</id>
<property name="secondElementKey" type="int" column="second_element_key" />
<set
name="secondElements"
table="second_element"
inverse="true"
lazy="false"
fetch="join"
cascade="all,delete-orphan" >
<key column="second_element_key" property-ref="secondElementKey" />
<one-to-many class="SecondElement" />
</set>
</class>
Class 2 : Definition
---------------------
<class name="SecondElement" table="second_element"
dynamic-update="true"
dynamic-insert="true"
select-before-update="true" >
<id name="serialNum" column="serial_num" unsaved-value="undefined" >
<generator class="native"/>
</id>
<property name="secondElementKey" type="int" column="second_element_key" />
<property name="url" type="string" column="url" not-null="true" />
</class>
Class 3 : Definition
---------------------
<class name="LastSecondElement" table="last_second_element"
dynamic-update="true"
dynamic-insert="true"
select-before-update="true" >
<id name="key" column="key" unsaved-value="undefined" >
<generator class="native"/>
</id>
</class>
Thanks
Rahul
|