English 中文(简体)
A. 对许多人来说,国籍标准
原标题:Nhibernate criteria API for many-to-many

我的目标模式如下:

项目有许多塔格,塔格人可以属于许多项目

我愿使用标准进行以下调查。

SELECT * FROM Item item
WHERE item.Id in (Select it.ItemId from dbo.ItemToTags it where it.Tag_id =  ONE )
AND item.Id in (Select it.ItemId from dbo.ItemToTags it where it.Tag_id =  TWO )

我愿提供一套可能的标签,然后提供所有这些标签的所有物品:

我尝试如下,但我没有结果:

CreateCriteria<Item>().CreateAlias("Tags", "Tags");
if (AndQuery) {
   foreach(var tag in Tags)
   {
      criteria.Add(Subqueries.PropertyEq("Tags.Id", DetachedCriteria.For<Tag>().Add(Restrictions.Eq("Id", tag))                                                   .SetProjection(Projections.Property("Id"))));
   }
} 
问题回答

我不敢确定,你的询问能否被翻译为:

SELECT * FROM Item item 
join ItemToTags itt on itt.ItemId = item.Id
join Tags t on itt.TagId = t.Id
where t.Id in ( ONE , TWO )

如果是的话,你应当能够做到:

CreateCriteria<Item>().CreateAlias( Tags , t )
   .Add(Restrictions.In( t.Id ,new List<string>{ TWO , ONE }).List<Item>();

假设你掌握了该项目的收集图。

h)





相关问题
nHibernate one-to-many inserts but doesnt update

Instead of getting into code, I have a simple question. Default behavior for a simple one-to-many is that it inserts the child record then updates the foreign key column with the parent key. Has ...

How Do I copy an existing nhibernate object as a new object?

I a persisted NHibernate object that I would like to repersist as a new entity. How do I get NHibernate to save this object as if it was a new? I am thinking I might create a session interceptor to ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

NHibernate Search in a List using ICriteria

I have my class X : public class ClassX { public virtual IList<ClassY> ListY { get; set; } ... } My ClassX mapping (using Fluent) ... HasMany<ClassX>(x => x.ListY ) ....

热门标签