NHibernate多对多标准任务标签示例

时间:2011-11-27 04:03:59

标签: hibernate nhibernate many-to-many

我在任务和标签之间建立了简单的多对多关系。任务可以具有许多标签,并且可以将标签分配给许多任务。我用三个表来管理它 - Task,Tag和TaskTag。到目前为止一切都很好。

我正在尝试为以下示例编写条件查询...

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

我找到了以下我试图遵循的例子。

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));

这是它产生的SQL ......

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
)

由于某些原因我无法清楚地理解,参数变得错位。

  1. 请注意,它将count(2)放在“in”列表中,并且
  2. 然后将第二个标记ID放在计数应该去的位置。
  3. 当我修改SQL以将参数放在正确的位置时,它可以工作。

    我必须有不正确的标准,但我看不到它。

    我感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:2)

这是我的解决方案

实体:

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; }
}

不要考虑实体&lt;&gt;。因为我们已经实现了IEquatable接口

,所以是通用的

下一个映射:

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();
    }
}

我设置级联只是为了节省保存任务的节奏。

使用我的标准的存储库:

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>();
    }

传递给我的单元测试:

    [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"));
    }

并且sql语句是:

    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

此致 /离子