-->
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.  [ 6 posts ] 
Author Message
 Post subject: one-to-one: Hibernate implementation of some design patterns
PostPosted: Fri Nov 14, 2003 7:08 am 
Beginner
Beginner

Joined: Thu Oct 09, 2003 11:41 am
Posts: 39
Location: Paris, France
Hi:

I have three tables in a DB: User, Employee, and Consultant. Each Employee or Consultant is a User. The table User contains basic data pertinent to both Employees and Consultants (such as the name, e-mail address, username, etc.). The tables Employee and Consulant contain other relevant data elements. When an employee or a consultant logs in, the login is checked against User, and then we work with their tables.

Employee-User and Consultant-User have one-to-one relationships, and the ID for an Employee/Consultant is the same as the ID for the User.

What I would like to have is a mapping to:

Code:
public class User implements Serializable {
...
}

public class Employee extends User implements Serializable {
...
}

public class Consultant extends User implements Serializable {
...
}


I've had a look at the test files that come bundled with Hibernate, but found no indication at to how that could be implemented (incl. a correct Hibernate2 implementation of the one-to-one relationship)

I don't mind changing the DB/Class design either, if you have such suggestions.

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 14, 2003 7:25 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
A one-to-one between two tables is easy enough to do, but I do not think you can use the same method for a "duel" one-to-one, as the ID of the "child' (i.e. Foreign generator) references the ID of the "parent".

A thought...perhaps define an abstract user Object, which Consultant and Employee each extend. Now...hmm...the mapping.

Perhaps look at using Subclass for Consultant and Employee within the User mapping...not sure.

-G


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 14, 2003 7:36 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
It looks like what you want is a <joined-subclass> mapping? no?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 14, 2003 11:06 am 
Beginner
Beginner

Joined: Thu Oct 09, 2003 11:41 am
Posts: 39
Location: Paris, France
This is it, <joined-subclass/> thanks!

One detail though - how do I change the default RESTRICT to CASCADE?

For example, this generates the following sql command (for SAP DB)

Code:
alter table tb_employee foreign key FK.... (user_id) references tb_user


It should be something like

Code:
alter table tb_employee foreign key FK.... (user_id) references tb_user ON DELETE CASCADE


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 14, 2003 11:10 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No, Hibernate will take care of deleting all entries.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 14, 2003 11:23 am 
Beginner
Beginner

Joined: Thu Oct 09, 2003 11:41 am
Posts: 39
Location: Paris, France
okay, thanks again.


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