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.