-->
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.  [ 4 posts ] 
Author Message
 Post subject: Many-To-One? One-To-One? Help Please.
PostPosted: Mon Oct 04, 2004 11:00 pm 
Regular
Regular

Joined: Sun Sep 26, 2004 9:27 pm
Posts: 75
Location: Atlanta, GA, USA
Everyone,

I need your help understanding how to use Hibernate to combine different objects/tables into a class.

Assume the following are tables and objects.

User
- id (primary key)
- name

UserProfile
- userId (foreign key to User.id)
- ssn

UserBalance
- userId (foreign key to User.id)
- money

This example is very simple, and I realize the three things could/should be combined into one table. But please bear with me.

Assume I want a fourth object used in presenting results from a user search. It holds the following:

UserSearchResult
- User.id
- User.name
- UserProfile.ssn
- UserBalance.money

This fourth object does not map directly to a table. All it does is encapsulate information from a query.

How do I represent this in Hibernate? I


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 05, 2004 12:27 am 
Newbie

Joined: Mon Oct 04, 2004 2:40 pm
Posts: 1
Location: Boulder, Colorado, USA
Quote:
the Many-To-One (which I think I need to use) definition really seems like a misnomer to me.


I think you may be interpreting the term many-to-one incorrectly. In object terminology, this always refers to multiplicity. So it describes an association relationship between to objects, where one side of the association has many instances of a given type, and the other side has one instance of another type:

Code:
  [ Payment ]-- * ----------- 1 -->[ PaymentType ]



This terms does not describe turning multiple tables into a single class, such as you desire to create in your example of user parts. To do this I think you may do best with joined subclasses. I think you will need one subclass per table type. This could be your hierarchy:

Code:
  User
    UserProfile
      UserAccount


But in my opinion, you may do better with the following class composition:

Code:
  User
    UserProfile
    UserAccount


I suggest this because profile information has little to do with a finanical account, really. But since your example may simply be abstract, the first hierarchy would give you what you are trying to do.

Just a suggestion. I hope I have not misunderstood your question.

Vaughn


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 05, 2004 12:36 am 
Regular
Regular

Joined: Sun Sep 26, 2004 9:27 pm
Posts: 75
Location: Atlanta, GA, USA
vvernon wrote:
I suggest this because profile information has little to do with a finanical account, really. But since your example may simply be abstract, the first hierarchy would give you what you are trying to do.

Just a suggestion. I hope I have not misunderstood your question.

Vaughn


Nope, I think you understood perfectly.

I think the full answer is, "Hibernate can't do *exactly* what I want it to.

No problems. I'll just write a SQL query, and load the object the old fashioned way.

Thank you very much for the response.

Lukas


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 05, 2004 2:10 am 
Expert
Expert

Joined: Thu Jan 29, 2004 2:31 am
Posts: 362
Location: Switzerland, Bern
I guess the following Hibernate 3 feature could do the job:

Quote:
immutable entities may now be mapped to SQL (from-clause) subselects, instead of tables (useful if you would like to have a database view, but don't)


But I don't know if this is already done and how to use it :-(.

HTH
Ernst


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