-->
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.  [ 5 posts ] 
Author Message
 Post subject: One-To-One mapping with an association table
PostPosted: Tue Feb 01, 2005 10:23 am 
Newbie

Joined: Wed Jan 05, 2005 2:28 pm
Posts: 14
Location: Germany, Munich
Hibernate version: 3.0 beta 2

I was wondering how to map one-to-one relations with an association table. I have two completely different types of users. I have only one login table. The login table is associated to these two types of user-tables with two association tables.

Example:

Code:
-- Usertype A
CREATE TABLE user_a (
    id int4 NOT NULL,
    some_info varchar(50),
    PRIMARY KEY (id)
);

-- Usertype B
CREATE TABLE user_b (
    id int4 NOT NULL,
    some_different_info varchar(50),
    PRIMARY KEY (id)
);

CREATE TABLE login (
    id int4 NOT NULL,
    name varchar(50) NOT NULL,
    password varchar(40) NOT NULL,
    PRIMARY KEY (id),
    UNIQUE (name)
);

CREATE TABLE user_a_login (
    user_id int4 NOT NULL,
    login_id int4 NOT NULL,
    UNIQUE (user_id),
    UNIQUE (login_id),
    FOREIGN KEY (user_id) user_a(id),
    FOREIGN KEY (login_id) login(id)
);

CREATE TABLE user_b_login (
    user_id int4 NOT NULL,
    login_id int4 NOT NULL,
    UNIQUE (user_id),
    UNIQUE (login_id),
    FOREIGN KEY (user_id) user_b(id),
    FOREIGN KEY (login_id) login(id)
);



Of course, usually this would be a many-to-many mapping, but I have a UNIQUE() for each - user_id and login_id within user_a_login and user_b_login to avoid many-mapping.

So, how can I map these tables and still be able to do a

Code:
Login = ((User_A)session.get(User_A.class, 1)).getLogin();


I don't want to use a Set, because there cannot be more than one entry.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 01, 2005 6:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Use <join> in Hibernate3


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 02, 2005 11:43 am 
Newbie

Joined: Wed Jan 05, 2005 2:28 pm
Posts: 14
Location: Germany, Munich
gavin wrote:
Use <join> in Hibernate3


I don't know how <join> could help here. I read the reference and see that it can be used to get the data of two tables into one class. But I have an association table in the middle - how could <join> help here?

What I need is a many-to-many mapping without the requirement of using a Collection, because it isn't a.


BTW: Is there a <join> for H3 annotations?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 02, 2005 6:28 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
<join> is know as @SecondaryTable in annotations

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 02, 2005 8:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I know what you want to do. Use <join> for it.


http://blog.hibernate.org/cgi-bin/blosx ... /join.html


That blog shows a one-to-many. One-to-one is similar, but with a <join> on both sides, one side is inverse="true", the other side is optional="true".


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