-->
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: Restrictions.ne() on a list of attributes
PostPosted: Tue Apr 29, 2008 7:13 am 
Beginner
Beginner

Joined: Fri Apr 11, 2008 1:48 am
Posts: 36
Hello world,

I got an problem:

I have got a Bean (for example: Person) which gots an one-to-many relation to another bean (for example: clothes). So I got within my bean person a list of clothes.

I do want to create an criterion, which has a restriction: Restrictions.ne("clothes.name", "shirt")

I want to get a list of persons, who does not have a shirt in their clothes list.

The result seems to be wrong: I don't get persons who have only shirts in their clothes list. If they have other clothes and shirts, they will be shown in the result list.


Top
 Profile  
 
 Post subject: Restrictions.ne() on a list of attributes
PostPosted: Fri May 02, 2008 5:53 am 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

I think you need to use a correlated subquery to get the results you're after. In NHibernate it would be:

Code:
DetachedCriteria shirts =
    DetachedCriteria.For(typeof(Person), "SubPerson")
        .Add(Expression.EqProperty("SubPerson.id", "Person.id"))
        .CreateAlias("Clothes", "Clothes")
        .Add(Expression.Eq("Clothes.Name", "Shirt"))
        .SetProjection(Projections.Property("Clothes.Name"));

IList<P> peopleWithoutShirts =
    session
        .CreateCriteria(typeof(Person), "Person")
        .Add(Subqueries.NotExists(shirts))
        .List<P>();


I'm not sure what the Hibernate equivalent is, but it will be pretty close to this.

Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 03, 2008 12:22 pm 
Beginner
Beginner

Joined: Fri Apr 11, 2008 1:48 am
Posts: 36
Thank you very much. I did post this thread in the wrong section. Your solution seems to be working, but I get an error message. See here:

http://forum.hibernate.org/viewtopic.ph ... 62#2384262


Top
 Profile  
 
 Post subject: Restrictions.ne() on a list of attributes
PostPosted: Sat May 03, 2008 1:31 pm 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

I think the reason you're getting an error is that you need to alias the main query and the sub-query with different aliases, then correlate them.

So, the main criteria needs an alias ('Person' in my example), and the detached criteria has a different alias ('SubPerson' in my example), and the detached criteria needs to join on these, so the key part of the query is:

Code:
.Add(Expression.EqProperty("SubPerson.id", "Person.id"))


Hope that helps.

Regards,
Richard


Top
 Profile  
 
 Post subject: Re: Restrictions.ne() on a list of attributes
PostPosted: Sat May 03, 2008 2:06 pm 
Beginner
Beginner

Joined: Fri Apr 11, 2008 1:48 am
Posts: 36
FlukeFan wrote:
Hi,

I think the reason you're getting an error is that you need to alias the main query and the sub-query with different aliases, then correlate them.

So, the main criteria needs an alias ('Person' in my example), and the detached criteria has a different alias ('SubPerson' in my example), and the detached criteria needs to join on these, so the key part of the query is:

Code:
.Add(Expression.EqProperty("SubPerson.id", "Person.id"))


Hope that helps.

Regards,
Richard


This does not work. My DetachedCriteria does not know the alias of the criteria. Or do I have to create an alias for the detached critiera in my main criteria?

Tried this:
Code:
Criteria crit = _session.createCriteria(Person.class, "con");
      
      DetachedCriteria buy =
          DetachedCriteria.forClass(Person.class, "dc")
             .add(Restrictions.eq("dc.id", "con.id"))
              .createAlias("clothes", "bi")
              .add(Restrictions.eq("bi.name", "shirt"))
              .setProjection(Projections.id());

      crit.add(Subqueries.notExists(buy));


Thank you very much...


Top
 Profile  
 
 Post subject: Re: Restrictions.ne() on a list of attributes
PostPosted: Sat May 03, 2008 2:59 pm 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi,

Yes, that looks correct (or at least it looks the same as mine). If that still doesn't work, you could try posting the whole exception + mapping (either here, or in the other thread).

Regards,
Richard


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 05, 2008 4:25 am 
Beginner
Beginner

Joined: Fri Apr 11, 2008 1:48 am
Posts: 36
Hi FlukeFan,

please have a look at this thread:
http://forum.hibernate.org/viewtopic.ph ... 54#2384354

Problem solved, but still far away from my target :-(


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.