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.