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.  [ 7 posts ] 
Author Message
 Post subject: many-to-many relationships without a join table, possible?
PostPosted: Fri May 06, 2005 12:44 pm 
Newbie

Joined: Thu May 05, 2005 10:36 am
Posts: 13
Location: England
I have been struggling for some time to shoehorn a legacy database into Java via hibernate, but have now come across a situation where i have a many-to-many relationship, but NO join table. Unfortunatly I cannot create a join table, so my question is, does hibernate allow me to create this many-to-many relationship, and if so, how?

I only need to access data from one side, so i was tempted to create a uni directional one to many, but then I would be breaking the contract as specified by the user docs (no entity can be in more than one collection at any one time or something similar).

Please dont tell me to RTFM - I have been studying them for quite a while now, including external ones on the web.[/b]


Top
 Profile  
 
 Post subject: Re: many-to-many relationships without a join table, possibl
PostPosted: Fri May 06, 2005 3:52 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
eXtorn wrote:
I have been struggling for some time to shoehorn a legacy database into Java via hibernate, but have now come across a situation where i have a many-to-many relationship, but NO join table. Unfortunatly I cannot create a join table, so my question is, does hibernate allow me to create this many-to-many relationship, and if so, how?

I only need to access data from one side, so i was tempted to create a uni directional one to many, but then I would be breaking the contract as specified by the user docs (no entity can be in more than one collection at any one time or something similar).

Please dont tell me to RTFM - I have been studying them for quite a while now, including external ones on the web.[/b]


You'll probably get help quicker if you provide the data and object models (or a simplified version of them).


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 07, 2005 9:49 pm 
Newbie

Joined: Thu May 05, 2005 10:36 am
Posts: 13
Location: England
Erm, sure... I didnt realise that that was neccesary given my query.

I have 2 tables, let us call them table A and table B.

A
col1 - PK
col2
col3

B
col1 - PK
col2 - PK
col3

Table A is joined to table B using "where A.col2 = B.col2"


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 08, 2005 10:43 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
I may not have the full picture but I see this as a one-to-many. A = parent and B = children


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 09, 2005 4:08 am 
Newbie

Joined: Thu May 05, 2005 10:36 am
Posts: 13
Location: England
Yes, I can understand why you have that viewpoint. Indeed, I shared the same viewpoint until the tables were explained to me by one of our domain experts.

Basically, table A contains some user defined questions used to filter results on some related searches, say for example 'is VAT liable', or 'is archived'. Table B contains a list of amongst other things, a list of valid responses for all the questions.

In the example below, it is clear that the YES and NO response would be valid to be returned as the associated responses to both queries located within tableA.

A simplified version of the table contents for the above example would be as follows:
Code:
TABLE A:

ROW 1:

PK  col1: VL
    col2: Is VAT liable
    col3: Some other information of no relevance here
FK  col4: NY

ROW 2:

PK  col1: IA
    col2: Is Archived
    col3: Some other information of no relevance here
FK  col4: NY

TABLE B:

ROW 1:

PK,FK  col1: NY
PK     col2: n
       col3: NO

ROW 2:

PK,FK  col1: NY
PK     col2: y
       col3: YES
[/i]


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 09, 2005 4:27 am 
Newbie

Joined: Thu May 05, 2005 10:36 am
Posts: 13
Location: England
Oh, something I should have mentioned. This database contains not one single explicitly defined Primary Key or foreign key, so the object of the hibernate exercise is to decouple our new appliation from this detestable database structure and thus allow us to change it in the future (when we have coerced our customers onto the new application by whatever means required).

I apologise for the somewhat piecemeal approach to providing information on the problem, but it has only really become clear to me the precise nature of the problem and the table design over the weekend.

I place hope of finding a solution in your hands.

:-)


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 09, 2005 12:26 pm 
Newbie

Joined: Thu May 05, 2005 10:36 am
Posts: 13
Location: England
I have just discovered that we do have a full FK - PK mapping for these tables, so the schema is now better represented as the following, however, the original question remains. - Can we map a many to many relationship using hibernate without a join table?

At the moment, the way I am getting around this problem is to do a lookup on TABLE A, then retrieving the appropriate properties from the object returned, and adding them as restrictions to TABLE B.

It would be nicer to be able to just reference the objects created using the contents of TABLE B through a set within the object created using TABLE A.

Code:
TABLE A:

ROW 1:

PK  col1: VL
    col2: Is VAT liable
    col3: Some other information of no relevance here
FK  col4: NY
FK  col5: y

ROW 2:

PK  col1: IA
    col2: Is VAT liable
    col3: Some other information of no relevance here
FK  col4: NY
FK  col5: n


There is

TABLE B:

ROW 1:

PK     col1: NY
PK     col2: n
       col3: NO

ROW 2:

PK     col1: NY
PK     col2: y
       col3: YES


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