-->
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.  [ 3 posts ] 
Author Message
 Post subject: many-to-many using a join table, mapped to non-id
PostPosted: Fri Oct 21, 2005 1:41 pm 
Newbie

Joined: Fri Oct 21, 2005 1:37 pm
Posts: 2
Hello,

Trying to map a many-to-many relationship like so,

table A:
A_id int
...

table B:
B_id int,
another_column varchar

A_B
A_id int,
another_column varchar

So, the join on table B should be done on another_column rather than B_id.

How does one map this?

Thanks,
Dan


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 5:49 pm 
Beginner
Beginner

Joined: Wed Jun 01, 2005 3:22 pm
Posts: 38
Location: Menlo Park, CA
If another_column has a UNIQUE constraint, then you might be able to get away with something using a "foreign-key" attribute somewhere.

If not, you map it as what it is... not a real many-to-many relationship. It's simply a table with zero-to-one (uni-directional one-to-one) with A and a column that happens to share a value with a column in B.

Don't fight the Object Relational Mapper by trying to model was isn't really there. Instead, model the reality of the system.

I would make a new class, AtoBStringMapping, with a one-to-one relationship to A and a string value for AnotherColumn.

Looking things up then becomes a matter of structuring a query that does a join on all the appropriate classes. It's more tedious than just accessing the many-to-many collections though object properties, but you're less likely to get into trouble down the road.

In my opinion, if the DB is not using real referential integrity constraints, then your mapping shouldn't pretend it is. You'll get away with it at first, but when some nutball goes in and changes a value in a column that everyone assured you was unique and could be used as a foreign key... well, your whole application will break in very intersting and dangerous ways. (And "but they said that would never happen!!! Inconceivable!!!" doesn't go over well with your boss)

If you can gaurantee that your app will be the only one that ever modifies the data, you can get away without DB-level constraints a little more easily.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 21, 2005 2:26 pm 
Newbie

Joined: Fri Oct 21, 2005 1:37 pm
Posts: 2
Thanks for the help,

They actually went with this structure against my advice, and while I assumed nhibernate probably wouldn't support this it was worth a shot.

The issue is, unsurprisingly, integrating with legacy systems! Anyway, since it was a read only sort of relationship, I just added a method to the containing class to get the objects that were related to it in this way.

Thanks again,
Dan


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