-->
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: Mapping two tables to one object
PostPosted: Mon Jul 03, 2006 9:40 am 
Newbie

Joined: Tue Apr 18, 2006 4:54 am
Posts: 9
Hibernate mapping allows you to define two objects coming from the same table

For example, I can easily have two POJOS, say, Person and Name

Code:
Person
------
dateOfBirth: Date
name: Name
...
------


and

Code:
Name
------
firstname: String
lastname: String
------


mapped to one table

Code:
PERSON
------
PERSON_ID    INTEGER    PRIMARY KEY
DATE_OF_BIRTH    DATETIME
FIRST_NAME    VARCHAR
LAST_NAME VARCHAR
-----


The question is, if I have a complex (e.g. long, but flat) POJO, can I map it across two tables.

For example, one POJO called Person

Code:
Person
------
dateOfBirth
name: Name
firstname: String
lastname: String
...
------


mapped to two tables PERSON and NAME with the same primary key

Code:
PERSON
------
PERSON_ID    INTEGER    PRIMARY KEY
DATE_OF_BIRTH    DATETIME
-----


and

Code:
NAME
------
PERSON_ID    INTEGER    PRIMARY KEY
FIRST_NAME    VARCHAR
LAST_NAME VARCHAR
-----


Hibernate version: Any


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 12:15 pm 
Beginner
Beginner

Joined: Wed Jun 21, 2006 10:08 am
Posts: 26
In short, no.

You can come up with a clever strategy to have a one-to-many mapping. Such that 1 person can have many names, such as a rock star having aliases or a person having nick names.

Hope that helps.

_________________
- Jonathan


Top
 Profile  
 
 Post subject: Views in the database?
PostPosted: Mon Jul 03, 2006 12:35 pm 
Newbie

Joined: Tue Apr 18, 2006 4:54 am
Posts: 9
That is a shame.

The reason I ask is because I am trying to figure out the best way to migrate an application from ATG Dynamo (http://www.atg.org) to a Spring and Hibernate based platform, and their ORM framework (ATG Repositories) does allow you to do this.

Perhaps one way to do it would be to use a view in the database to join the two tables - thus presenting a single 'table' for the Hibernate mapping.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 12:44 pm 
Newbie

Joined: Fri Jun 09, 2006 8:03 am
Posts: 12
If your person didn't have a name it could easily be done with the join tag, here is a good tutorial/example:
http://blog.exadel.com/?p=16 (Scroll down to The Joined Tables Pattern)

If name needs to be in there, you might be able to use composite patterns that are talked about in the reference documentation. I haven't really used composite classes so I don't know.


Top
 Profile  
 
 Post subject: Oops. That was a typo
PostPosted: Wed Jul 12, 2006 5:15 am 
Newbie

Joined: Tue Apr 18, 2006 4:54 am
Posts: 9
Sorry. That was a typo. The POJO does not need a name property if it has firstname and lastname

e.g. it would be

Code:
Person
------
dateOfBirth
firstname: String
lastname: String
...
------


and not

Code:
Person
------
dateOfBirth
name: Name
firstname: String
lastname: String
...
------


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.