Hi there,
I'm new to nHibernate . For practice, I'm using it to develop a DVD tracking program, with SQL Server 2000.
This program allows a user to keep a list of DVDs and to assign tags to these DVDs, and later search on these tags. Tags can be things like "Comedy", "TV", "Film", "Thriller", "Performance" and so on. There is a many-to-many relationship between DVDs and tags. So a DVD of the film "Sideways" would have a Tags collection containing the "Film" and "Comedy" tags, among others.
My problem is this. I want users to be able to do an "AND" search on the tags, so if a user searched on "Film" and "Comedy", only those DVDs with both "Film"
and "Comedy" tags in their tags collections would be returned. I've been using ICriteria so far, but have also looked at IQuery. Either way I can't see a way to achieve what I need.
I've included some sample code below, using a Finder-based approach (thanks M Fowler). This returns an empty list every time, when I know it should return DVD's based on the tags I supply. Any ideas anyone? Any help would be much appreciated...
Thanks
Paul
Code:
using System;
using System.Collections.Generic;
using NHibernate;
using NHibernate.Expression;
using Pabs.Data.NHibernate;
namespace DVDTracker.Data.NHibernate
{
public class DVDFinder : Finder <Title>, ITitleFinder
{
private Nullable<Int32> m_yearOfRelease = null;
private IList <Tag> m_tags = null;
public Nullable<Int32> YearOfRelease
{
get { return m_yearOfRelease; }
set { m_yearOfRelease = value; }
}
public IList <Tag> Tags
{
get { return m_tags; }
set { m_tags = value; }
}
protected override void SetCriteria (ICriteria criteria)
{
if (m_yearOfRelease.HasValue)
{
criteria.Add (Expression.Eq ("YearOfRelease", m_yearOfRelease.Value));
}
if (m_tags != null && m_tags.Count > 0)
{
ICriteria tagCriteria = criteria.CreateCriteria ("Tags");
foreach (Tag tag in m_tags)
{
tagCriteria.Add (Expression.Eq ("Id", tag.Id));
}
}
}
}
}