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 "Not In" Query
PostPosted: Fri Oct 13, 2006 11:53 am 
Newbie

Joined: Fri Mar 03, 2006 8:05 am
Posts: 14
I have a many-to-many relationship for two tables (A,B).

I've written an ICriteria query to return a list of all the B's that link to a single A id thus:

ICriteria criteria = session.CreateCriteria(typeof(B));
criteria.CreateCriteria("LinkedAs").Add(Expression.Eq("ID", aId));

I need to do the inverse and return a list of all the B's that *don't* link to A but I'm struggling to get the right criteria.

At first I tried this:

criteria.CreateCriteria("LinkedAs").Add(Expression.Not(Expression.Eq("ID", aId)));

but this didn't work.

I'd be really grateful for some help as this problem is now giving me a major headache!

Thanks[/list]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 18, 2006 2:39 am 
Newbie

Joined: Fri Mar 03, 2006 8:05 am
Posts: 14
In the absence of any replies I have implemented the following function:

public List<T> GetByFk<fkT, fkIdT>(fkIdT id, string propertyName, bool inverse)
{
ICriteria criteria = session.CreateCriteria(persitentType);
criteria.CreateCriteria(propertyName).Add(Expression.Eq("ID", id));
List<T> list = ConvertToGenericList(criteria.List());
if (inverse)
{
List<T> all = GetAll();
foreach(T obj in list)
{
all.Remove(all.Find(delegate(T t)
{
return t.Equals(obj);
}));
}
list = all;
return list;
}

Please help me find a better way without needing an expensive loop!


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.