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: one-to-one newbie question
PostPosted: Fri Aug 25, 2006 6:12 pm 
Newbie

Joined: Fri Aug 25, 2006 5:46 pm
Posts: 1
Hi,

I'm having two tables

User:

UserID (PK)
name
pictureID (FK)


Picture:

PictureID (PK)
Path

Every user has one picture and every picture is owned by only one user.
I'm not quite sure how to map and implement this - I'm a bit confused about the explaination in the tutorial. Even though there is no real code example.

My mapping intends as they are stored in the DB:

<class name="UserItem" table="User">
<id name="UserID">
<column name="UserID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex" />
</id>
<property name="Name" length="32" not-null="true" />
<property name="PictureID" length="32" not-null="true" />
</class>

<class name="PictureItem" table="Picture">
<id name="PictureID">
<column name="PictureID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex" />
</id>
<property name="Path" length="32" not-null="true" />
</class>


My implementation intends regarding DB and Mapping:

class UserItem
{
private string userID; // and properties
private string name; // and properties
private string pictureID; // and properties
}

class PicturItem
{
private string pictureID; // and properties
private string path; // and properties
}


Ok, so far I can play with the tables seperately, but it would be more elegant to include the one-to-one relationship like in the DB to exctract for example one object containing all data.

How do I do this?

May I change

<property name="PictureID" length="32" not-null="true" />


to


<one-to-one name="PictureItem">
<column name="PictureID" length="32" not-null="true" />
</one-to-one>

or something like this?
And how to implement this in the Classes?

Thanks for any help

Michael


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 26, 2006 11:37 pm 
Beginner
Beginner

Joined: Thu Nov 11, 2004 12:18 pm
Posts: 37
Location: Baltimore, MD
This example from the documentation should give you what you need. In this case there are two objects, Person and Employee. Employee has it's own PK generated (not shown), and Person gets it's primary key based on the employee assigned to it.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Primary key associations don't need an extra table column; if two rows are related by the association then the two table rows share the same primary key value. So if you want two objects to be related by a primary key association, you must make sure that they are assigned the same identifier value!

For a primary key association, add the following mappings to Employee and Person, respectively.

Code:
<one-to-one name="person" class="Person"/>


Code:
<one-to-one name="employee" class="Employee" constrained="true"/>


Now we must ensure that the primary keys of related rows in the PERSON and EMPLOYEE tables are equal. We use a special Hibernate identifier generation strategy called foreign:

Code:
<class name="person" table="PERSON">
    <id name="id" column="PERSON_ID">
        <generator class="foreign">
            <param name="property">employee</param>
        </generator>
    </id>
    ...
    <one-to-one name="employee"
        class="Employee"
        constrained="true"/>
</class>

_________________
-Chris


Top
 Profile  
 
 Post subject: Does this work with annotations
PostPosted: Fri Feb 15, 2008 12:16 pm 
Newbie

Joined: Thu Feb 14, 2008 1:41 pm
Posts: 14
TheXFed,

I've trying to solve this problem since a long time, Read your post on Hibernate JIRA too
http://opensource.atlassian.com/project ... e/HHH-2712 You seem to have solved the problem, but when a few of us tried the same were unsuccessful...

http://forum.hibernate.org/viewtopic.ph ... 23#2376623
AND
http://forum.hibernate.org/viewtopic.ph ... 41#2376741

Would it be possible for you to post a detailed document of how it worked, and what were the configuration you used ?


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.