-->
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.  [ 4 posts ] 
Author Message
 Post subject: Reference a property of a property in a criteria expression
PostPosted: Mon Jun 27, 2005 5:48 am 
Newbie

Joined: Mon Jun 27, 2005 5:12 am
Posts: 10
Hi

I have a couple of classes, CatalogItem and Artist. An Artist may have many CatalogItems so I've setup a Many-to-One relationship in my CatalogItem mapping file as shown below.

Code:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="CatalogItem,Entity" table="CatalogItem">
      <meta attribute="class-description"></meta>
      <jcs-cıche usage="read-write"/>

      <id name="_CatalogItemId" column="CatılogItemId" type="Guid" unsaved-value="{00000000-0000-0000-0000-000000000000}">
         <meta attribute="field-description"></meta>
         <generıtor class="guid.comb"/>
      </id>
      <many-to-one name="_Artist" class="Artist,Entity" column="ArtistId"  />
   
   </class>
</hibernate-mapping>



Here's the Artist class mapping file:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="Artist,Entity" table="Artist">
      <meta attribute="class-description"></meta>
      <jcs-cache usage="read-write"/>

      <id name="_ArtistId" column="ArtistId" type="Guid" unsaved-value="{00000000-0000-0000-0000-000000000000}">
         <meta attribute="field-description"></meta>
         <generator class="guid.comb"/>
      </id>
      <property name="_Name" column="Name" type="String" not-null="true" length="255">
         <meta attribute="field-description"></meta>
      </property>
      <property name="_Description" column="Description" type="String" length="1073741823">
         <meta attribute="field-description"></meta>
</property>

   </class>
</hibernate-mapping>


Now I want to get a list of CatalogItems by artist id using the following code

Code:
public IList GetCatalogItemsForArtist(IArtist artist)
      {
         ISession session = null;
         bool closeSession = false;
         if (null == session)
         {
            session = SessionFactory.OpenSession();
            closeSession = true;
         }
         IList list = session.CreateCriteria(CatalogItem.GetType())
            .Add(SimpleExpression.Eq("_Artist.ArtistId", artist.ArtistId))
            .List();
         
         if (closeSession)
         {
            session.Flush();
            session.Close();
         }
         return list;
      }



With the above example I get an unresolved property error for _ArtistId

I've looked around and seen somebody asking about an Criteria.AddAlias call in the Hibernate documentation but there's no equivalent in NHibernate.

How do I reference the ArtistId a property of the _Artist property in my criteria expression?

Any help much appreciated :O)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 27, 2005 9:17 am 
Expert
Expert

Joined: Fri May 13, 2005 5:56 pm
Posts: 308
Location: Santa Barbara, California, USA
not a complete answer to your question, however, you do not need to reference the ArtistID property really. this works equally as well:

Code:
IList list = session.CreateCriteria(CatalogItem.GetType())
            .Add(Expression.Eq("_Artist", artist))
            .List();


sionce you're already passing the entire artist object into the method...

-d


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Tue Jun 28, 2005 11:21 am 
Newbie

Joined: Mon Jun 27, 2005 5:12 am
Posts: 10
Hi

Excellent, thanks for the heads up on that, I had actually gone down the IQuery route (see below), but I'll use your suggestion instead, looks neater.

Code:
string typeName = CatalogItem.GetType().ToString();
         IQuery query = session.CreateQuery("from " + typeName + " where ArtistId = (:artistId)");
         query.SetParameter("artistId", artist.ArtistId);
         IList list = query.List();
[/code]


Top
 Profile  
 
 Post subject: Re: Reference a property of a property in a criteria express
PostPosted: Wed Jun 29, 2005 2:34 pm 
Expert
Expert

Joined: Fri May 13, 2005 11:13 am
Posts: 292
Location: Rochester, NY
Distilling to relevant sections...
Quote:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<class name="Artist,Entity" table="Artist">
   <id name="_ArtistId" column="ArtistId" type="Guid" unsaved-value="{00000000-0000-0000-0000-000000000000}">
   </id>
</class>


Now I want to get a list of CatalogItems by artist id using the following code

Code:
         IList list = session.CreateCriteria(CatalogItem.GetType())
            .Add(SimpleExpression.Eq("_Artist.ArtistId", artist.ArtistId))
            .List();

...
How do I reference the ArtistId a property of the _Artist property in my criteria expression?

... it seems that you should be referencing "_Artist._ArtistId", not "_Artist.ArtistId". It looks like you are mapping against private properties which are named differently from your public class interface. Try seeing what you can do with the "access" attribute, that will allow the names you use for HQL to match those in you classes.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.