-->
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.  [ 1 post ] 
Author Message
 Post subject: mapping two foreign key cols to same table; view objects
PostPosted: Sat Mar 20, 2004 1:24 pm 
Newbie

Joined: Fri Mar 12, 2004 1:14 am
Posts: 1
Location: New York City
I have a few general questions about using Hibernate to persist objects, or more
accurately to re-create persisted objects that are related in complex
ways and to retrieve data from the database to place in objects
representing views--a subset of information from one or more tables.

In case it is pertinent, I'm using Hibernate 2.1 and MySQL 4.0. I've looked through
the Hibernate2 Reference Documentation, and I've searched the FAQs and Forums.
If I've overlooked an answer somewhere, please forgive and just tell me where
to look.

Suppose I have a system that models an email client. I'd like to have
objects for users and messages. A full user record, in a user object
or in the user table, would include username, password, lots of other
personal information, and a unique id. The message table would include
subject, date, message text, and sender and recipient columns
containing user ids serving as foreign keys, as well as a unique id.

Question 1:
How do I set up a mapping file for Message? There are two columns that
contain foreign keys to the user table: sender_id and recipient_id. In
the message.hbm.xml file, I set up two many-to-one elements:
<many-to-one
name="sender"
column="sender_id"
class="User"
cascade="none"
update="false"
insert="false" />
<many-to-one
name="recipient"
column="recipient_id"
class="User"
cascade="none"
update="false"
insert="false" />
Hibernate complains that I'm associating with User twice. In a SQL
select, I would alias the user table so I could include it twice:
SELECT ....
FROM message, user sender, user recipient
WHERE ....
AND sender.id = message.sender_id
AND recipient.id = message.recipient_id

Is there some way to accomplish this aliasing in the mapping file?

Question 2:
A "retrieved" message object ideally would contain only as much information as
neccessary about the sending and receiving users: their usernames. I
could structure the Message class to contain two User objects, but
that seems messy and a little unwise, since User objects contain
personal information like passwords. Do I have to structure the
message object so it contains user objects? I could always create a
class called MessageDisplay or such that only contains the info I want
to send to the presentation layer, and I could transfer the info from
the Message objects to MessageDisplay objects, but that seems like an
inefficient extra step. I found a few very terse posts about creating views.
How do I do this to retrieve just the message info I want, perhaps to generate
a different class object?

Question 3:
Users of the email client would be able to view a mailbox with message
summaries: date, sender, recipient, title. As in the previous question, there's
no need to retrieve all the users' information or even the text of the
messages, which could be lengthy. I only need a little bit of info
from the user and message tables. Do I have to recreate the message
and user objects and then transfer the needed info to smaller, flatter
objects? Or is there a way to "map" a class like MessageSummary to a
view of data from other persistent objects? What's the best practice here?

Thanks for reading this far! I really like Hibernate, and I hope to
make it work throughout my application. Again, if I need to RTFM, that's
fine--would someone point me to the appropriate FM?

:-)

Thanks!

Michael


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.