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-Many where clause
PostPosted: Tue Jul 01, 2008 6:48 am 
Newbie

Joined: Tue Jul 01, 2008 6:30 am
Posts: 1
Hello,
I'm playing around with NHibernate, trying to map some of my tables. I have a many to many relationship which uses 2 tables A and B, A is connected to A (again) via B. B also contains a column to specify the kind of relationship.

So, briefly:
A: ID, SomeThing
B: FromID, ToID, TypeOfRelationship

I already added a bag to my mapping file to query for "children" objects. Now I want to add a filter, to pull up only children having TypeOfRelationship = SomeValue.
I suppose I have to use where in XML, but when I put where="TypeOfRelationship=1" the resultant SQL shows TableA_Alias.TypeOfRelationship = 1. It have to look for TypeOfRelationship field in table B, not A. Also, forcing B.TypeOfRelationship = 1 in where clause, outputs literealy the same where in SQL, but due to the fact that A and B are both aliased in TableA_Alias and TableB_Alias, B is an unknown table in query.

In the sample below you can see: "where item1_.RelationshipType=1" but I need to produce "where costs0_ .RelationshipType=1"

How can I solve this?

TIA, mt


Hibernate version:
1.2.1

Mapping documents:
Code:
<bag name="Costs" table="ItemRelationships" lazy="true" >
      <key column="ItemRelationshipID"></key>
      <many-to-many column="FromItemID" class="Item" where="RelationshipType=1" />
    </bag>


The generated SQL (show_sql=true):
Code:
FROM ItemRelationships costs0_ left outer join Items item1_ on costs0_.FromItemID=item1_.ItemID WHERE  ( item1_.RelationshipType=1 ) and costs0_.ItemRelationshipID=@p0; @p0 = '1'


Name and version of the database you are using:
MS SQL 2005


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 02, 2008 11:03 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can't have additional columns in a many-to-many table (at least hibernate can't use them). You can define a class Relationship which contains both ids and the relationship type.

_________________
--Wolfgang


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.