nhibernate 多对多查询
我是 nhibernate 的新手,并尝试在数据库上创建一个查询,其中项目和类别之间有许多链接。
我有一个包含3个表的数据库:项目、类别和查找表类别项目,如下所示:
categorys - 主键categoryId
items - 主键itemId
categoryItem - CategoryId 列和 itemId 列
我想要一个查询返回特定类别的项目,并且已经尝试过这个,并且认为我的做法是正确的:
public IList<Item> GetItemsForCategory(Category category)
{
//detached criteria
DetachedCriteria itemIdsCriteria = DetachedCriteria.For(typeof(Category))
.SetProjection(Projections.Distinct(Projections.Property("Item.Id")))
.Add(Restrictions.Eq("Category.Id", category.Id));
criteria.Add(Subqueries.PropertyIn("Id", itemIdsCriteria));
return criteria.List<Item>() as List<Item>;
}
我仅具有类别和项目的业务对象。 如何创建存储库方法来查找特定类别的项目?
I am new to nhibernate and trying to create a query on a database with manytomany links between items and categories.
I have a database with 3 tables : items, categories and a lookup table categoryitem like this:
categorys - primary key categoryId
items - primary key itemId
categoryItem - categoryId column and itemId column
I want a query returning items for a particular category and have tried this and think i am along the right lines:
public IList<Item> GetItemsForCategory(Category category)
{
//detached criteria
DetachedCriteria itemIdsCriteria = DetachedCriteria.For(typeof(Category))
.SetProjection(Projections.Distinct(Projections.Property("Item.Id")))
.Add(Restrictions.Eq("Category.Id", category.Id));
criteria.Add(Subqueries.PropertyIn("Id", itemIdsCriteria));
return criteria.List<Item>() as List<Item>;
}
I only have business objects for category and item.
how do i create a repository method to find items for a particular category?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设你的类看起来像这样:
查询(HQL)
标准
I assume that your classes look like this:
query (HQL)
Criteria