-->
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: Many-to-many mapping without join table
PostPosted: Thu Apr 02, 2009 2:35 pm 
Regular
Regular

Joined: Fri Feb 09, 2007 3:47 pm
Posts: 56
I've been wrestling with an issue for a while, and am not sure what the best option is.

I've got a set of C# classes Aclass and Bclass. and SQL Server tables Atable and Btable.

Aclass
--------
int ID
String AField1
ISet<Bclass> ASet1
...other fields...

Bclass
-------
int ID
String BField1
ISet<Aclass> BSet1
...other fields...

ATable
-------
int ID
varchar Acolumn1
...other fields...

BTable
-------
int ID
varchar Bcolumn1
...other fields...

The IDs are the primary keys.
The Sets contain links to the classes (A and B) that have the same value for AField1 and BField1. So, for example, if an instance of Aclass and Bclass had the same value in their Field1, that instance of Aclass would be referenced in BSet1, and vice versa.


So it's a many-to-many relationship, but there's no join table, and since neither AField1 nor BField1 are unique keys in their table, there's no foreign key reference.

Also, unfortunately, simply modifying the database schema is not possible.

Is there some way to mimic a join table, or a workaround to relate ATable and BTable via their Column1s without modifying the database?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 04, 2009 3:32 am 
Regular
Regular

Joined: Wed Feb 11, 2009 10:58 am
Posts: 55
Code:
<set name="ASet1" table="Bclass" >
  <key column="AColumn1"/>
  <many-to-many column="BColumn1" class="Bclass">
</set>


Maybe something like that will work or does that need to have a foreign key between the two tables?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 04, 2009 2:37 pm 
Regular
Regular

Joined: Fri Feb 09, 2007 3:47 pm
Posts: 56
reflection wrote:
Code:
<set name="ASet1" table="Bclass" >
  <key column="AColumn1"/>
  <many-to-many column="BColumn1" class="Bclass">
</set>


Maybe something like that will work or does that need to have a foreign key between the two tables?


I'm not sure if that will work, as I'm not at a work computer right now, but my understanding was that
Code:
<set ... table="tablename">
represented the join table.. The table for the class the hbm is in, isn't really needed, as it's already in there in
Code:
<class ... table="tablename">


Anyways. I'll try it on Monday, and let you know how it goes.

Thanks :)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 05, 2009 2:28 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
If that does not work, you can try and use a one-to-many on both ends.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 06, 2009 11:32 am 
Regular
Regular

Joined: Fri Feb 09, 2007 3:47 pm
Posts: 56
Okay, apparently I'm reassigned. Won't be able to test it, unfortunately. Looks like in <set>, "table=" is optional. It probably defaults to ATable in this case, so I'm guessing that that's the trick. I'm going to close this :)

Thanks for the help, guys.


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