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)