-->
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.  [ 2 posts ] 
Author Message
 Post subject: Many to One Query
PostPosted: Fri Jun 24, 2005 12:52 pm 
Newbie

Joined: Wed Apr 27, 2005 1:30 pm
Posts: 7
Hi There,

This is prob a basic question but finding it hard to find a good answer.

I have three tables.

A user table
A menu table

a user_to_menu table.

The user_to_menu table just contains an ID, USERID,MENUID,

Sampel Date

id userid menuid
2 5 1
2 5 2
2 5 3
2 5 4

I think i am wrong in the way i am populating this object.

First i save a user and get his generated ID
Then i retrieve the menu id's he entitled to eg 1,2,3,4

Then a i create 4 usertomenu objects with user id the same but menu id different and save this to the database.

While it achieves what i need, i dont think it is the best way to do it?

Any tips

_________________
Tell me, I'll forget. Show me, I may remember. But involve me, and I'll understand.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 7:52 pm 
Regular
Regular

Joined: Thu May 26, 2005 12:20 am
Posts: 72
I would suggest reading through the Hibernate In Action book to understand the concepts of hibernate better. I don't necessarily understand things completely, but here is my take on it:

You dont ever deal with IDs when you are working with hibernate. the mapping table of [id, id, id] will be entirely transparent to your code. You will do something like this, assuming User<->Menu is a bi-directional many-to-many relationship:

Code:
(pseudocode)
User u = getUser(id);
Menu m1 = new Menu();
u.addToMenus(m1);
m1.addToUsers(u);
Menu m2 = new Menu();
u.addToMenus(m2);
m2.addToUsers(u);
saveUser(u);


then your hibernate XML file is set up to tell hibernate how to save that down to the right tables, including the ID mapping table and what type of cascading to do. The book has examples.

dan


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