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.  [ 1 post ] 
Author Message
 Post subject: Need Help searching associated list
PostPosted: Tue Dec 05, 2006 5:38 pm 
Newbie

Joined: Tue Dec 05, 2006 5:15 pm
Posts: 1
Location: London, England
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));
                }
            }
        }
    }
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.