gpt4 book ai didi

java - 如何使用 Spring 通过多个可选条件和 OneToMany 关系进行查找

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:50 24 4
gpt4 key购买 nike

我尝试使用 QueryDsl 来解决这个问题,但它不起作用,我在这个问题 Spring data and QueryDsl, how to find by multiple criteria in one row and a OneToMany relationship 中提出了这个问题。

问题是我有两张 table ,一个人及其衣服。

PersonID |PersonFirstname|PersonLastname
========================================
1 |Jack | Jackson
2 |Kelly | Kellson

那么 Person 表可以是(OneToMany by PersonId)

ID | PersonID | Jacket | Color | Size
========================================
1 |1 |Nike | black | XL
2 |1 |ADIDAS | white | XL
3 |1 |Prada | green | L
4 |1 |ADIDAS | blue | XL

5 |2 |Reebock | yellow| S
6 |2 |Majestic| brown | XS
7 |2 |Prada | green | M

From my other question: If I now write a query like this

findPersonWho.and(person.personClothes.any().jacket.eq("Nike")
.and(person.personClothes.any().color.eq("black"))
.and(person.personClothes.any().size.eq("L")));

It will return me the Person with the ID 1, because he has a jacket from nike and a color black and a size L.

But I only want Patient 1 if he has a Jacket from nike with the color black and the size L.

现在我尝试用另一种方式做到这一点,我编写了一个新方法来仅在衣服表中进行搜索,如下所示

if (jacket != null) {
findPerson.and(p.jacket.eq("Nike").and(p.ecolor.eq("black").and(p.size.eq(L))));
}

这是可行的,但如果我现在想使用 BooleanBuilder 在此表中按多个条件查找,例如更多穿着 Reebock 夹克的人,颜色为黄色,尺寸如下:

if (jacket1 != null) {
findPerson.and(p.jacket.eq("Nike").and(p.ecolor.eq("black").and(p.size.eq(L))));
}
if (jacket2 != null {
findPerson.and(p.jacket.eq("Reebock").and(p.ecolor.eq("yellow").and(p.size.eq(S))));

这将返回 0,但实际上有两个匹配项。但我认为他现在是通过 ID 搜索,而不是通过 PationID 搜索。所以没有 ID 1 与展位匹配..

有什么办法可以进行这个搜索吗?也许用其他方法?

最佳答案

尝试关注并将搜索替换为您想要搜索的内容

Criteria person = sf.getCurrentSession().createCriteria(Person.class).createAlias("personId", "personId");

person.add(Restrictions.disjunction().add(Restrictions.ilike("PersonFirstname",Search,MatchMode.ANYWHERE))
.add(Restrictions.ilike("personId.Jacket",Search,MatchMode.ANYWHERE))
.add(Restrictions.ilike("personId.Color",Search,MatchMode.ANYWHERE))
.add(Restrictions.ilike("personId.Size",Search,MatchMode.ANYWHERE)));

关于java - 如何使用 Spring 通过多个可选条件和 OneToMany 关系进行查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37443544/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com