-->
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: ICriteria, Expressions, and Empty Collections
PostPosted: Thu Jul 07, 2005 7:12 pm 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
hoping this is an easy one for you veterans:

Parent Class (Asset) with a Collection of Children (MemberAssets). Child class is an association class if that matters. I'm tryig to use ICriteria to find an IList of Assets with certain properties, one of them being a MemberAssets collection that is not empty. I though this might work:

Code:
public IList GetOwnedAssets(Member owner, bool isArchived, string orderByField)
{
   return this.dbSession.CreateCriteria(typeof(Asset))
      .Add(Expression.Eq("Owner", owner))
      .Add(Expression.Eq("IsArchived", isArchived))
      .Add(Expression.IsNotNull("MemberAssets"))
      .AddOrder(Order.Asc(orderByField))
      .List();
}


but it doesn't. This makes sense actually because my constructor(s) initialized the array:

Code:
protected Asset()
{
   this.id = new Guid("00000000-0000-0000-0000-000000000000");
   this.version = -1;
   this.dateCreated = DateTime.Now;
   this.lastUpdate = DateTime.Now;
   this.memberAssets = new ArrayList();
}

protected Asset(string title, Member owner)
{
   this.id = new Guid("00000000-0000-0000-0000-000000000000");
   this.version = -1;
   this.title = title;
   this.owner = owner;
   this.dateCreated = DateTime.Now;
   this.lastUpdate = DateTime.Now;
   this.memberAssets = new ArrayList();
}


I could remove the assignment from the constructor but would still need it in the property:

Code:
public virtual IList MemberAssets
{
   get
   {
      if (this.memberAssets == null)
         this.memberAssets = new ArrayList();

      return this.MemberAssets;
   }
}


so either way MemberAssets is never going to be NULL. Is there any to do this using ICriteria? Essentially, I think I'm trying to check the MemberAssets.Count property in the query...

Thanks in advance,

-devon


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 10, 2005 11:47 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
AFAIK, you can't do it with ICriteria... You must use the function count(...) in your HQL

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 10, 2005 12:27 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Criteria queries actually have nothing to do with the way you code properties in C#, they are translated into SQL directly.

I think what you want to do is not possible currently, maybe it will become possible once ICriteria.CreateCriteria is implemented, but I'm not sure.


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.