Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6882549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:16:02+00:00 2026-05-27T05:16:02+00:00

I have a simple many to many relationship set up between tasks and tags.

  • 0

I have a simple many to many relationship set up between tasks and tags. A task may have many tags and a tag may be assigned to many tasks. I use three tables to manage this – Task, Tag, and TaskTag. So far so good.

I am trying to write a criteria query for the following example…

Find all tasks that are tagged with 'Apple' (id = 1718) AND 'Orange' (id = 1717)

I found the following example which I tried to follow.

NHibernate many-to-many criteria

// Create sample list of tag ids.
var tagIDs = new List<long>();
tagIDs.Add(1718);
tagIDs.Add(1717);

// Create criteria and detached criteria.
var criteria = Session.CreateCriteria<Task>();
var detachedCriteria = DetachedCriteria.For<Task>("t")
    .SetProjection(Projections.GroupProperty(Projections.Id()))
    .Add(Restrictions.Eq(Projections.Count(Projections.Id()), tagIDs.Count))
    .Add(Restrictions.EqProperty("t.ID", "ID"))
    .CreateCriteria("Tags")
    .Add(Restrictions.In("id", tagIDs.ToArray<long>()));
criteria.Add(Subqueries.Exists(detachedCriteria));

This is the SQL it produces…

SELECT this_.ID as ID4_0_, [MORE COLS HERE]
FROM dbo.[Task] this_
WHERE exists (
    SELECT this_0_.ID as y0_
    FROM dbo.[Task] this_0_
    inner join dbo.TaskTag tags3_ on this_0_.ID=tags3_.TaskID
    inner join dbo.[Tag] tag1_ on tags3_.TagID=tag1_.ID
    WHERE this_0_.ID = this_0_.ID
    and tag1_.ID in (2, 1718)                   <------ 1
    GROUP BY this_0_.ID
    HAVING count(this_0_.ID) = 1717             <------ 2
)

For some reason I cannot clearly understand, the parameters are getting misplaced.

  1. Notice that it puts the count (2) in the “in” list incorrectly, and
  2. It then puts the second tag id where the count should go.

When I modify the SQL to put the parameters in the correct locations, it works.

I must have the criteria incorrect, but I cannot see it.

I appreciate any help. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:16:03+00:00Added an answer on May 27, 2026 at 5:16 am

    Here is my solution

    Entities:

    public class TaskTest : Entity<TaskTest>
    {
        public virtual string Name { get; set; }
    
        public virtual IList<TagTest> Tags { get; set; }
    
        public TaskTest()
        {
            Tags = new List<TagTest>();
        }
    }
    
    public class TagTest : Entity<TagTest>
    {
        public virtual string Name { get; set; }
    }
    

    Do not take in the consideration Entity<>. Is is generic as we’ve implemented a IEquatable interface

    Next Mappings:

    public class TaskTestMap : EntityMap<TaskTest>
    {
        public TaskTestMap()
        {
            Map(x => x.Name).Not.Nullable().Length(512);
            HasManyToMany(x => x.Tags).Table("TaskTag").Cascade.All();
        }
    }
    
    public class TagTastMap : EntityMap<TagTest>
    {
        public TagTastMap()
        {
            Map(x => x.Name).Not.Nullable();
        }
    }
    

    I set cascade just to save tasgs on saving tasks.

    The repository with my criteria:

    public IList<TaskTest> GetTaskByTagIds(IList<long> tagIds)
        {
            DetachedCriteria exists = DetachedCriteria.For<TaskTest>("t")
                .CreateAlias("t.Tags", "tags")
                .Add(Restrictions.EqProperty("t.Id", "task.Id"))
                .Add(Restrictions.In("tags.Id", tagIds.ToArray()))
                .SetProjection(Projections.GroupProperty("t.Id"))
                .Add(Restrictions.Eq(Projections.Count("t.Id"), tagIds.Count));
    
            ICriteria criteria = GetSession().CreateCriteria<TaskTest>("task")
                .Add(Subqueries.Exists(exists));
    
            return criteria.List<TaskTest>();
        }
    

    And the unit test which pass on my side:

        [Test]
        public void TestTaskTest()
        {
            //Insert data;
    
            var task1 = new TaskTest {Name = "task1"};
            var task2 = new TaskTest {Name = "task2"};
            var task3 = new TaskTest {Name = "task3"};
    
            var tag1 = new TagTest {Name = "tag1"};
            var tag2 = new TagTest {Name = "tag1"};
            var tag3 = new TagTest {Name = "tag1"};
    
            task1.Tags.Add(tag1);
            task1.Tags.Add(tag2);
    
            task2.Tags.Add(tag1);
            task2.Tags.Add(tag3);
    
            task3.Tags.Add(tag3);
    
    
            _repository.AddToSession(task1);
            _repository.AddToSession(task2);
            _repository.AddToSession(task3);
    
            FlushAndClearSession();
    
            //We will try to get all task which a taged with tag1 and tag2. The result should be task1 
    
            var tagsId = new List<long> {tag1.Id, tag2.Id};
    
            var result = _repository.GetTaskByTagIds(tagsId);
    
            Assert.That(result, Is.Not.Null);
            Assert.That(result, Is.Not.Empty);
            Assert.That(result.Count, Is.EqualTo(1));
            Assert.That(result[0].Name, Is.EqualTo("task1"));
        }
    

    and the sql statement is:

        exec sp_executesql N'SELECT this_.Id as Id50_0_, this_.Version as Version50_0_, this_.Name as Name50_0_ 
        FROM [TaskTests] this_ 
        WHERE exists (
                        SELECT this_0_.Id as y0_ 
                        FROM [TaskTests] this_0_ 
                        inner join TaskTag tags3_ on this_0_.Id=tags3_.TaskTestId 
                        inner join [TagTests] tags1_ on tags3_.TagTestId=tags1_.Id 
                        WHERE this_0_.Id = this_.Id and tags1_.Id in (@p0, @p1) 
                        GROUP BY this_0_.Id 
                        HAVING count(this_0_.Id) = @p2)',
    N'@p0 bigint,@p1 bigint,@p2 int',
    @p0=9000,@p1=9001,@p2=2
    

    Regards,
    /Ion

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables set up in a many-to-many relationship: Incidents and Users. When
I have a simple one-to-many (models.ForeignKey) relationship between two of my model classes: class
So essentially I have two tables, containing URLS and TAGS, with a has-and-belongs-to-many relationship
I have a simple many-to-many relationship between the User class and the Place class.
I have a simple has_many through relationship set up: class Tag < ActiveRecord::Base has_many
I have a simple one-to-many relationship. I would like to select rows from the
I have a simple databasescheme: User, Account. User has 1-to-many relationship with Account. I
I have a Users table and a Networks table with a many-to-many relationship between
I have a simple 1:many aggregate relationship, lets say: public class Parent { public
I am trying to set up a simple one-to-many relationship where a list can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.