使用AND条件进行一对多搜索 [英] One to Many search using AND condition

查看:64
本文介绍了使用AND条件进行一对多搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下含有多种颜色的产品。



我希望找到至少包含RED和GREEN的产品。

 产品分类

字符串ID;

列表< Color>颜色{};

颜色类别

id

颜色

请勿忽略语法错误。

我可以使用以下搜索条件。

  Criteria criteria = createCriteria(); 
criteria.createAlias(colors,colors);

列表< String> colorsList = new LinkedList();
colorsList.add(GREEN);
colorsList.add(RED);
criteria.add(Restriction.in(colors.color,colorsList);

以上将给我产品的颜色有红色或绿色,但不包含至少包含红色和绿色的产品。



示例

 产品:红色绿色 - 通过
产品:红色绿色黄色 - 通过
产品:红色黄色 - 失败

$ b

预先致谢。

解决方案

我们的想法是,我们选择所有产品的颜色和每个产品的数量,然后两种颜色的产品应该有2的颜色数量计数

  DetachedCriteria colorCrit = DetachedCriteria.For(Product.class)
.createAlias(colors,color)
.add(Restriction.eq(color.color,RED )
.add(Restriction.eq(color.color,GREEN)
.SetProjection(Projections.Group(id))
.add(Restriction.eq( Projections.rowCount(),2));

条件标准a = createCriteria()
.add(Subqueries.in(id,colorCrit)
.list();

更新:

有一个问题为hibernate完全这一点。最后的评论描述了如何使用。


I have the following product which contain many colors.

I wish to find the product which contain at least RED and GREEN.

Product class

    String id;

    List<Color> colors{};

Color class

    id

    color

kindly ignore the syntax error.

I'm able to use the following to search OR condition.

Criteria criteria = createCriteria();
criteria.createAlias("colors","colors");

List<String> colorsList = new LinkedList();
colorsList.add("GREEN");
colorsList.add("RED");
criteria.add(Restriction.in("colors.color",colorsList);

The above will give me products which has red or green in their colors BUT not products which contain at least RED AND GREEN.

Example

Product: RED GREEN - PASS
Product: RED GREEN YELLOW - PASS
Product: RED YELLOW - FAIL

Thanks in advance.

解决方案

the idea is we select all products with the colors and count each product, then products with both colors should have a count of 2 as the number of colors

DetachedCriteria colorCrit = DetachedCriteria.For(Product.class)
    .createAlias("colors","color")
    .add(Restriction.eq("color.color", "RED")
    .add(Restriction.eq("color.color", "GREEN")
    .SetProjection(Projections.Group("id"))
    .add(Restriction.eq(Projections.rowCount(), 2));

Criteria criteria = createCriteria()
    .add(Subqueries.in("id", colorCrit)
    .list();

Update:

there is an issue for hibernate for exactly this. the last comment describes how to use.

这篇关于使用AND条件进行一对多搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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