Hibernate查询集合中的多个项目 [英] Hibernate query for multiple items in a collection

查看:86
本文介绍了Hibernate查询集合中的多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的数据模型:

  public class Item {
private List< ItemAttribute> ;属性;
//其他东西
}

公共类ItemAttribute {
私有字符串名称;
私有字符串值;

$ / code $ / pre
$ b $(这显然简化了很多不必要的东西)



我想要做的是创建一个查询来询问具有一个或多个特定属性的所有项目,理想情况下与任意的AND和OR连接。现在我保持简单,只是试图实现AND情况。在伪SQL中(或者假HQL),它会是这样的:

 选择所有项目$ b $ (ItemAttribute(name =foo1,value =bar1))
AND属性包含(ItemAttribute(name =foo2,value =bar2))

Hibernate文档中的例子似乎没有解决这个特殊的用例,但它看起来很常见。这种情况也是有用的,特别是我可以指定一个可能的值列表,例如

  where attributes contains(ItemAttribute(名称=foo,值=bar1))
OR属性包含(ItemAttribute(name =foo,value =bar2))
- 等

下面是一个适用于单个属性的示例:



<$ p $ (ItemAttributes,ia)
.add(Restrictions.conjunction()
)返回getSession()。createCriteria(Item.class)
.createAlias .add(Restrictions.eq(ia.name,foo))
.add(Restrictions.eq(ia.attributeValue,bar)))
.list();

学习如何做到这一点将有助于扩展我对Hibernate潜力的理解。 :)

解决方案

您可以使用别名来做到这一点吗?

 条件itemCriteria = session.createCriteria(Item.class); 
itemCriteria.createAlias(itemAttributes,ia1)
.createAlias(itemAttributes,ia2)
.add(Restrictions.eq(ia1.name,foo1 ))
.add(Restrictions.eq(ia1.attributeValue,bar1)))
.add(Restrictions.eq(ia2.name,foo2))
.add(Restrictions.eq(ia2.attributeValue,bar2)))

不知道hibernate如何处理两次明确地加入同一个属性,可能值得一试?


I have a data model that looks something like this:

public class Item {
    private List<ItemAttribute> attributes;
    // other stuff
}

public class ItemAttribute {
    private String name;
    private String value;
}

(this obviously simplifies away a lot of the extraneous stuff)

What I want to do is create a query to ask for all Items with one OR MORE particular attributes, ideally joined with arbitrary ANDs and ORs. Right now I'm keeping it simple and just trying to implement the AND case. In pseudo-SQL (or pseudo-HQL if you would), it would be something like:

select all items
where attributes contains(ItemAttribute(name="foo1", value="bar1"))
AND attributes contains(ItemAttribute(name="foo2", value="bar2"))

The examples in the Hibernate docs didn't seem to address this particular use case, but it seems like a fairly common one. The disjunction case would also be useful, especially so I could specify a list of possible values, i.e.

where attributes contains(ItemAttribute(name="foo", value="bar1"))
OR attributes contains(ItemAttribute(name="foo", value="bar2"))
-- etc.

Here's an example that works OK for a single attribute:

return getSession().createCriteria(Item.class)
        .createAlias("itemAttributes", "ia")
        .add(Restrictions.conjunction()
            .add(Restrictions.eq("ia.name", "foo"))
            .add(Restrictions.eq("ia.attributeValue", "bar")))
        .list();

Learning how to do this would go a long ways towards expanding my understanding of Hibernate's potential. :)

解决方案

Could you use aliasing to do this?

Criteria itemCriteria = session.createCriteria(Item.class);
itemCriteria.createAlias("itemAttributes", "ia1")
            .createAlias("itemAttributes", "ia2")
            .add(Restrictions.eq("ia1.name", "foo1"))
            .add(Restrictions.eq("ia1.attributeValue", "bar1")))
            .add(Restrictions.eq("ia2.name", "foo2"))
            .add(Restrictions.eq("ia2.attributeValue", "bar2")))

Not sure how hibernate handles joining on the same property twice explicitly like that, maybe worth trying?

这篇关于Hibernate查询集合中的多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆