tags:

views:

53

answers:

1

Class is defined as this:

class User {
     public int ID { get; set; }
     public string OpenID { get; set; }
     public IList<Tag> Tags { get; set; }
}

OpenID is set as natural-id, so that the second level cache recognizes this.

I have this HQL query which returns a list of user tags.

db.CreateQuery("select Tags from User where OpenID = :openId")
  .SetString("openId", openId)
  .List<Tag>();

As far as I know HQL does not have a syntax to identify OpenID as natural-id, but CriteriaQuery has that (Restrictions.NaturalId()...)

So I need to convert this query to CriteriaQuery. Something in this direction:

db.CreateCriteria<User>()
  .Add(Restrictions.NaturalId().Set("OpenID", openId))
  //I need to tell criteria query that I want to return Tags property here - I don't know how to do that
  .List<Tag>();
A: 
db.CreateCriteria<User>()
  .SetProjection(Projections.Property("Tags"));
  .Add(Restrictions.Eq("OpenID", openId));
  .List<Tag>();

Not sure about this NaturalId, never used it, I probably would just add a normal restriction.

Stefan Steinegger
It seems so logical that this would work, but it doesn't.It returns a list with one item and that item is null.Executed query is this: SELECT this_.ID as y0_ FROM [USER] this_ WHERE this_.OPENID = @p0;Mapping for Tags is like this: <bag name="Tags" table="USER_TAG"> <cache usage="nonstrict-read-write" /> <key> <column name="ID_USER" /> </key> <many-to-many class="BeriMe.Biz.Domain.Tag"> <column name="ID_TAG" /> </many-to-many> </bag>
dmonlord
If you're interested in NaturalId() you should read this: http://ayende.com/Blog/archive/2009/06/23/nhibernate-ltnatural-idgt.aspx
dmonlord