-->
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: Using a many-to-many table in a many-to-one Class
PostPosted: Wed Jan 23, 2008 11:05 am 
Beginner
Beginner

Joined: Tue Jan 22, 2008 11:33 am
Posts: 46
Hi,

I got 2 tables wich are related to each other in many-to-many relationship using a third Table
FIRSTTABLE
FirstId
FirstName

SECONDTABLE
SecondId
SecondName

JOINTABLE
FirstId
SecondId


FIRSTTABLE & SECONDTABLE are represented by Code-Classes. The JOINTABLE has no Code Class and serves only for associating both tables. Therefore, the Many-to-Many Relationship is formed through the mapping file.
Now I like to reference to the JOINTABLE with another Table which is also represented by a Class. The composite-key of the JOINTABLE will not be key-field in this Table.

CONSUMINGTABLE
KeyID
FirstId
SecondId

Anybody an idea on how to do the mapping in the mapping-file. For many-to-one mapping from CONSUMINGTABLE to JOINTABLE I do not have a class which I can refernce to?
I tried a lot and I urgently need some help with this issue.

Thanks in advance for all your effort and your input

Bye

antoschka


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 23, 2008 11:30 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
antoschka,

If the JoinTable starts acquiring business importance of its own, maybe it is time to make it a new entity. Then:

- FirstTable would have a one-to-many relationship with JoinTable
- JoinTable would have a many-to-one relationship with SecondTable

and
-ConsumingTable would have a many-to-one relationship with JoinTable

You will end by creating a class and mapping object also for JoinTable, even if it has nothing but the composite PK.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject: I was affraid of this answer
PostPosted: Wed Jan 23, 2008 11:46 am 
Beginner
Beginner

Joined: Tue Jan 22, 2008 11:33 am
Posts: 46
Hi,

thanks for thsi answer. I was afraid of it. I thought of this solution myself, but I wanted to keep my many-to-many relationship without Creating classes which are actually not need, except for the mapping. I think I go for that, but I would be very happy if somebody can offer a more elegant way, where I could continue working with my many-to-many relationship.

Thanks in advance for your input

antoschka


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 23, 2008 12:45 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
antoschka ,

This happens all the time, a nice many-to-many relationship that we want to keep simple, but it starts been needed elsewhere. Don't feel bad about creatig a new entity.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject: Yes, but how to solve it
PostPosted: Wed Jan 23, 2008 12:58 pm 
Beginner
Beginner

Joined: Tue Jan 22, 2008 11:33 am
Posts: 46
Hi,

Somehow I'm struggling with the solution. could somebody show me how to do it in this small example? I guess I have to work with composite-id or composite element, but I have no clue how to do it.
Maybe you could just comment my guesses and correct me if I'm wrong.
- For FirstClass the mapping should be just a simple one-to-many mapping.
- Same for SecondClass
- for JoinClass I need a many-to-one mapping FirstClass and another one to SecondClass, right?
- what do I have to define in JoinClass for consumingClass and vice versa? If it touches composite-keys/-ids how do I have to do that?

I thankful for all the help

antoschka


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.