-->
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: mapping 2 tables into one class
PostPosted: Mon Sep 20, 2004 8:55 am 
Regular
Regular

Joined: Mon Sep 20, 2004 8:42 am
Posts: 58
Location: Boston, US
I'm using Hibernate 2.1.3. The documents describe how to use "components" to map a single table into two classes (eg User, Address).

I'm working with a legacy database that has two tables that have a one-to-one relationship however the second table has just one additional column and instead of mapping these as 2 classes, I'd like to map these two tables into one class. Consider a User table and UserDetail table where the UserDetail table only has a "phone" column. I would like to map this to a single User class that in addition to the members corresponding to the User table, has a "phone" member as well.

Thanks,
Sanjiv


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 20, 2004 9:05 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
In Hibernate3, you can use the <join> element. In Hibernate 2 this is not possible, you will have to use something like a hidden one-to-one association.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 11, 2004 12:40 am 
Newbie

Joined: Thu Oct 28, 2004 9:36 pm
Posts: 9
Location: Melbourne, Australia
Michael,

Please could you elaborate on the solution for the "mapping 2 tables into one class" solution in Hibernate 2.1.x

I am using a legacy database and need to map two tables into a single class, it is also a one-to-one relationship. This is currently being done with a view which encapsulates a simple select with a join on primary keys.

I was hoping to move this join out to the hibernate mapping.

You mention
Quote:
hidden one-to-one association
... in regards to the example from sjivan... does this mean wrap the UserDetail.Phone column in a class itself and then have it as a member of the User class?

I have many columns in both tables and so this would get quite clumsy...

Thanks

_________________
Miles Johnson
Agentis Software


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 11, 2004 12:43 am 
Newbie

Joined: Thu Oct 28, 2004 9:36 pm
Posts: 9
Location: Melbourne, Australia
Sorry, my first post above was missing a paragraph? Here is it complete

Michael,

Please could you elaborate on the solution for the "mapping 2 tables into one class" solution in Hibernate 2.1.x

I am using a legacy database and need to map two tables into a single class, it is also a one-to-one relationship. This is currently being done with a view which encapsulates a simple select with a join on primary keys.

I was hoping to move this join out to the hibernate mapping.

You mention
Quote:
hidden one-to-one association

in regards to the example from sjivan... does this mean wrap the UserDetail.Phone column in a class itself and then have it as a member of the User class?

I have many columns in both tables and so this would get quite clumsy...

Thanks

_________________
Miles Johnson
Agentis Software


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 11, 2004 3:08 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
An other approach is to map the two tables to two calsses and make them looking as one by having a facade class above the two.

If you use a DAO pattern you could write a DAO for the facade in order to hide the two mapped classes from the business logic.

HTH
Ernst


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 17, 2004 1:09 am 
Newbie

Joined: Thu Oct 28, 2004 9:36 pm
Posts: 9
Location: Melbourne, Australia
ernst_pluess thanks for the help,

I have considered this as well and using views in the database... but both are moving the problem "out of" Hibernate. I was looking for a solution that I could configure in the mapping file.

Looks like Hibernate3 contains what I am really seeking...

http://www.hibernate.org/21.html#A26
http://blog.hibernate.org/cgi-bin/blosx ... /join.html

Thanks,
Miles


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.