-->
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.  [ 8 posts ] 
Author Message
 Post subject: Get object propertiy from other table
PostPosted: Thu Jun 22, 2006 10:38 am 
Newbie

Joined: Thu Jun 22, 2006 7:19 am
Posts: 14
I have the followin db schema:

Table Person:
- person_id: int
- name: string
- ...

Table fingerprint
- fingerprint_id: int
- person_id: int
- fingerprint_data: string

-----------------------------------------------------------------------------
And these are the class definitions:

public class Person {

private int person_id;
private String name;
...
private Fingerprint fingerprint;

}


public class Fingerprint {

private int fingerprint_id;
private Person person;
private String fingerprintData;

}


-----------------------------------------------------------------------------
So a person can have 0 or 1 fingerprint, and one fingerprint belongs to only one person. How dshould be declared this situation in the XML mapping file?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 22, 2006 6:44 pm 
Beginner
Beginner

Joined: Mon Jul 26, 2004 4:29 pm
Posts: 45
Location: TX, USA
Hi,

I set up your tables in HsqlDb using the following SQL:
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 (
   fingerprint_id identity primary key
  ,person_id int
  ,fingerprint_data varchar(50)
  ,foreign key (person_id) references person ( person_id ));
 

insert into person values (null, 'John Doe');
*person_id ~
call identity();

insert into fingerprint values (null, *{person_id}, 'Here is John''s fingerprint data.');

insert into person values (null, 'Sally Smith');
*person_id ~
call identity();

insert into fingerprint values (null, *{person_id}, 'Here is Sally''s fingerprint data.');

-- now a person without a fingerprint
insert into person values (null, 'Hank Williams');


Then I set up the following classes based on yours:

Code:
package hg;

public class Person {
    private int personId;
    private String name;
    private Fingerprint fingerprint;
   
    // getters and setters below
}

package hg;

Code:
public class Fingerprint {
    private int fingerprintId;
    private int personId;
    private String fingerprintData;
    private Person person;

    // getters and setters below
}

Here is the xml mapping:

Code:
   <class name="Person" table="PERSON">
        <id name="personId" column="PERSON_ID">
            <generator class="native"/>
        </id>
        <one-to-one name="fingerprint" class="Fingerprint" />
        <property name="name" />
    </class>

    <class name="Fingerprint" table="FINGERPRINT">
        <id name="fingerprintId" column="FINGERPRINT_ID">
            <generator class="native"/>
        </id>
        <one-to-one name="person"  class="Person" />
        <property name="fingerprintData" column="FINGERPRINT_DATA" />
    </class>

I then wrote some code to retrieve objects from either side of the association and this is the relavent output:

INFO Main - Let's list Persons then Fingerprints
INFO Main - Person is: hg.Person@12884e0[id=0,name=John Doe]
INFO Main - Associated fingerprint is: hg.Fingerprint@1b5340c[id=0,data=Here is John's fingerprint data.]
INFO Main - Person is: hg.Person@1ce3fc5[id=1,name=Sally Smith]
INFO Main - Associated fingerprint is: hg.Fingerprint@b307f0[id=1,data=Here is Sally's fingerprint data.]
INFO Main - Person is: hg.Person@cf66b[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@814013[id=0,data=Here is John's fingerprint data.]
INFO Main - Associated person is: hg.Person@1884174[id=0,name=John Doe]
INFO Main - Fingerprint is: hg.Fingerprint@1d2b01b[id=1,data=Here is Sally's fingerprint data.]
INFO Main - Associated person is: hg.Person@195d4fe[id=1,name=Sally Smith]


Notice that the person without a fingerprint (Hank Williams) comes back with a null for the associated fingerprint object.

Hope this helps,


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 23, 2006 2:48 am 
Newbie

Joined: Thu Jun 22, 2006 7:19 am
Posts: 14
Thank you very much! That is exactly what I need :)

¡Saludos desde España! / Greetings from Spain!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 23, 2006 4:27 am 
Newbie

Joined: Thu Jun 22, 2006 7:19 am
Posts: 14
I have talk too fast.

If I has this data:

Person person_id: 1
Person name: John

Fingerprint fingerprint_id: 9
Fingerprint person_id: 1
Fingerprint fingerprint_data: john data

(I am using Firebird db)

Then when i list the users, i see the user john, but without a fingerprint, because person_id and fingerprint_id are not equals. The field that indicates the person that owns a fingerprint is the column person_id on the fingerprint table, not the fingerprint_id.

I need to indicate in the one-to-one directive that the relation between the tables is that the person_id (not the fingerprint_id) on the fingerprint table corresponds to the person_id on the person table.

Any idea?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 23, 2006 8:04 am 
Beginner
Beginner

Joined: Mon Jul 26, 2004 4:29 pm
Posts: 45
Location: TX, USA
You're exactly right. I didn't get the mapping quite right, but my data hid that fact. Give me a little while and I'll re-work the example.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 23, 2006 11:05 am 
Beginner
Beginner

Joined: Mon Jul 26, 2004 4:29 pm
Posts: 45
Location: TX, USA
Here is new data with person_id's not equal to associated fingerprint_id's:

Code:
sql> select * from person;
PERSON_ID  NAME
---------  -------------
        0  John Doe
        1  Sally Smith
        2  Hank Williams

3 rows
sql> select * from fingerprint;
FINGERPRINT_ID  PERSON_ID  FINGERPRINT_DATA
--------------  ---------  ---------------------------------
             0          1  Here is Sally's fingerprint data.
             1          0  Here is John's fingerprint data.

2 rows



Here is the updated mapping:

Code:
    <class name="Person" table="PERSON">
        <id name="personId" column="PERSON_ID">
            <generator class="native"/>
        </id>
        <many-to-one name="fingerprint" column="PERSON_ID" unique="true"
        insert="false" update="false" not-found="ignore" />
        <property name="name" />
    </class>

    <class name="Fingerprint" table="FINGERPRINT">
        <id name="fingerprintId" column="FINGERPRINT_ID">
            <generator class="native" />
        </id>
        <one-to-one name="person" property-ref="fingerprint" />
        <property name="fingerprintData" column="FINGERPRINT_DATA" />
    </class>


Here is the output:

INFO Main - Let's list Persons then Fingerprints
INFO Main - Person is: hg.Person@13f210f[id=0,name=John Doe]
INFO Main - Associated fingerprint is: hg.Fingerprint@1fd5e2[fingerprintId=0,personId=1,data=Here is Sally's fingerprint data.]
INFO Main - Person is: hg.Person@c743eb[id=1,name=Sally Smith]
INFO Main - Associated fingerprint is: hg.Fingerprint@84cc09[fingerprintId=1,personId=0,data=Here is John's fingerprint data.]
INFO Main - Person is: hg.Person@3c342b[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@25c828[fingerprintId=0,personId=1,data=Here is Sally's fingerprint data.]
INFO Main - Associated person is: hg.Person@1a12495[id=0,name=John Doe]
INFO Main - Fingerprint is: hg.Fingerprint@77ef83[fingerprintId=1,personId=0,data=Here is John's fingerprint data.]
INFO Main - Associated person is: hg.Person@955cd5[id=1,name=Sally Smith]


Now the correct fingerprint is associated with the correct person from both directions.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 23, 2006 3:44 pm 
Beginner
Beginner

Joined: Mon Jul 26, 2004 4:29 pm
Posts: 45
Location: TX, USA
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,


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jun 24, 2006 6:20 am 
Newbie

Joined: Thu Jun 22, 2006 7:19 am
Posts: 14
OK. Many thanks for your effort and try to help me, even with my poor english. I'm going to try this.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.