I was looking at my results and I realized that I made another mistake. The association from fingerprint to person wasn't pulling the right data. (Look at the end of the output above.) I tried to get several different one-to-one mappings to work with the tables you provided, but couldn't. Hibernate wants to map a one-to-one to a primary key, not another field. I tried to use the formula attribute to specify another field, but couldn't find the right incantation.
I finally re-wrote the example taking out the fingerprint_id altogether.
The sql now looks like this:
Code:
-- drop tables (child table first)
drop table fingerprint;
drop table person;
-- person table
create table person (
person_id identity primary key
,name varchar(30) );
-- fingerprint table
create table fingerprint (
person_id int primary key
,fingerprint_data varchar(50)
,foreign key (person_id) references person ( person_id ));
insert into person values (null, 'John Doe');
*john_id ~
call identity();
insert into person values (null, 'Sally Smith');
*sally_id ~
call identity();
insert into fingerprint values (*{sally_id}, 'Here is Sally''s fingerprint data.');
insert into fingerprint values (*{john_id}, 'Here is John''s fingerprint data.');
-- now a person without a fingerprint
insert into person values (null, 'Hank Williams');
The mappings now look like this:
Code:
<class name="Person" table="PERSON">
<id name="personId" column="PERSON_ID">
<generator class="native"/>
</id>
<one-to-one name="fingerprint" />
<property name="name" />
</class>
<class name="Fingerprint" table="FINGERPRINT">
<id name="personId" column="PERSON_ID">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<one-to-one name="person" constrained="true" lazy="false" />
<property name="fingerprintData" column="FINGERPRINT_DATA" />
</class>
And the output now appears to be correct from both sides:
INFO Main - Let's list Persons then Fingerprints
INFO Main - Person is: hg.Person@178460d[id=0,name=John Doe]
INFO Main - Associated fingerprint is: hg.Fingerprint@17f242c[personId=0,data=Here is John's fingerprint data.]
INFO Main - Person is: hg.Person@1938039[id=1,name=Sally Smith]
INFO Main - Associated fingerprint is: hg.Fingerprint@1b06041[personId=1,data=Here is Sally's fingerprint data.]
INFO Main - Person is: hg.Person@c743eb[id=2,name=Hank Williams]
INFO Main - Associated fingerprint is: null
INFO Main - Let's list Fingerprints then Persons
INFO Main - Fingerprint is: hg.Fingerprint@b1074a[personId=0,data=Here is John's fingerprint data.]
INFO Main - Associated person is: hg.Person@1eec35[id=0,name=John Doe]
INFO Main - Fingerprint is: hg.Fingerprint@55a338[personId=1,data=Here is Sally's fingerprint data.]
INFO Main - Associated person is: hg.Person@d85cc[id=1,name=Sally Smith]
I apologize for the wrong info. Maybe someone more experienced than me could show me how to take the original tables and map the one-to-one properly?
Cheers,