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.  [ 3 posts ] 
Author Message
 Post subject: Foreign key based upon identity in parent
PostPosted: Tue Aug 30, 2005 5:52 pm 
Newbie

Joined: Tue Aug 30, 2005 4:43 pm
Posts: 8
I have a scenario where I have a parent and child relationship. The parent's primary key is an identity and there is a one to many relationship between the parent and the child. How do you tell Hibernate to propogate the generated identity in the parent to the foreign key field in the child? I currently have in the parent mapping the following:

<set name="Profiles" inverse="true">
<key>
<column name="ParentID" not-null="true" />
</key>
<one-to-many class="Profile" />
</set>

and in the child mapping

<many-to-one name="parent" class="Parent">
<column name="ParentID" not-null="true" />
</many-to-one>

Unfortunately I receive a NullPointerException on the last line of code in the following code snipit:

Parent par = new Parent();
par.setFirstName("Joe");
par.setLastName("Blow");
par.setCreatedOn(new Date());

Profile prof = new Profile();
prof.setCreatedOn(new Date());
prof.setProfileName("Joe Blow Profile Name");

par.getProfiles().add(prof);

Any help would be much appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 31, 2005 2:02 pm 
Newbie

Joined: Tue Aug 30, 2005 4:43 pm
Posts: 8
I was able to solve this problem and have the identity value propogate to the child object by making the following changes:

- in the mapping in the parent file I had to put the full path to the profile class and added a new attribute called "cascade" in the "set" element with a value of "save-update". It looks like this now:

<set name="profiles" inverse="true" cascade="save-update">
<key>
<column name="ParentID" not-null="true" />
</key>
<one-to-many class="com.peopleclick.eval.hibernate.Profile" />
</set>

In the Profile mapping file I changed the many-to-one element to look as follows:

<many-to-one name="parent" class="com.peopleclick.eval.hibernate.Parent">
<column name="ParentID" not-null="true" />
</many-to-one>

Finally I modified my code to use a HashSet as follows:

Parent par = new Parent();

par.setFirstName(firstName);
par.setLastName(lastName);
par.setCreatedOn(new Date());

Profile prof = new Profile();

prof.setCreatedOn(new Date());
prof.setProfileName(firstName + " " + lastName + " Profile Name");
prof.setParent(par);

HashSet hs = new HashSet();
hs.add(prof);
par.setProfiles(hs);

s.save(par);


Once I made these changes, the identity that was created in the db for the parent class was propogated to the underlying Profile class.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 31, 2005 2:13 pm 
Newbie

Joined: Tue Aug 30, 2005 4:43 pm
Posts: 8
Now for the next layer of the onion. In my previous example I had a Parent and Profile with the relationship between the two being a one to many. In the underlying db, the Profile table had a foreign key that pointed to an identity column in the Parent table. Taking my evaluation one step further, I've introduced a Record class (and table) that has a one to many relationship to the Profile class. Here again, the foreign key in the Record table references an identity in the Profile table. The difference now being that the primary key of the Record table is a composite key of the ProfileId foreign key and a RecordId an integer (non-identity). Unfortunately the logic I used to solve the previous problem does not work in this situation. Apparently the composite key acts somewhat differently and the save fails since it is expecting a value for the ProfileID field in the Record table. The newly added mappings look as follows:

Profile Mapping:

Code:
    <class name="com.peopleclick.eval.hibernate.Profile" table="Profile" schema="dbo" catalog="Homework">
        <id name="profileId" type="integer">
            <column name="ProfileID" />
            <generator class="identity" />
        </id>
        <many-to-one name="parent" class="com.peopleclick.eval.hibernate.Parent">
            <column name="ParentID" not-null="true" />
        </many-to-one>
        <property name="profileName" type="string">
            <column name="ProfileName" length="50" not-null="true" />
        </property>
        <property name="createdOn" type="timestamp">
            <column name="CreatedOn" length="16" not-null="true" />
        </property>
        <set name="records" inverse="true" cascade="save-update">
            <key>
                <column name="ProfileID" not-null="true" />
            </key>
            <one-to-many class="com.peopleclick.eval.hibernate.Record" />
        </set>
    </class>


Record Mapping:

Code:
    <class name="com.peopleclick.eval.hibernate.Record" table="Record" schema="dbo" catalog="Homework">
        <composite-id name="id" class="com.peopleclick.eval.hibernate.RecordId">
            <key-property name="recordId" type="integer">
                <column name="RecordID" />
            </key-property>
            <key-property name="profileId" type="integer">
                <column name="ProfileID" />
            </key-property>
        </composite-id>
        <many-to-one name="profile" class="com.peopleclick.eval.hibernate.Profile" update="false" insert="false">
            <column name="ProfileID" not-null="true" />
        </many-to-one>
        <property name="createdOn" type="timestamp">
            <column name="CreatedOn" length="16" not-null="true" />
        </property>
    </class>


The code is as follows:
Code:
         SessionFactory sessionFactory;
         sessionFactory = new Configuration().configure().buildSessionFactory();
               
         Session s = sessionFactory.openSession();
         
         Transaction tx = s.beginTransaction();
         
         Parent par = new Parent();

         par.setFirstName(firstName);
         par.setLastName(lastName);
         par.setCreatedOn(new Date());
      
         Profile prof = new Profile();
         HashSet hs = new HashSet();

         Record rec = new Record();
         rec.setCreatedOn(new Date());
         RecordId id = new RecordId();
         id.setRecordId(1);
         rec.setId(id);
         rec.setCreatedOn(new Date());
         rec.setProfile(prof);
         
         
         prof.setCreatedOn(new Date());
         prof.setProfileName(firstName + " " + lastName + " Profile Name");
         prof.setParent(par);
         hs = new HashSet();
         hs.add(rec);
         prof.setRecords(hs);
         
         hs = new HashSet();
         hs.add(prof);      
         
         par.setProfiles(hs);
            
         s.save(par);
          tx.commit();
          s.close();      


I was hopeful the same methodology would work that solved my previous problem, but so far no luck... Any help would be appreciated.


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

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.