-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: How to generate a Unique Value for one-to-many element key?
PostPosted: Mon Dec 12, 2005 6:51 pm 
Newbie

Joined: Mon Dec 12, 2005 6:41 pm
Posts: 1
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.