gpt4 book ai didi

java - 条件查询返回[]

转载 作者:行者123 更新时间:2023-12-02 01:28:04 31 4
gpt4 key购买 nike

当某些类字段可能为空时,我尝试使用条件存储库按类字段查找数据库记录。我正在检查字符串、整数,并且使用 SQL 中的“like”来检查二进制字中的位。

我的实体类和元模型都在单独的包中:

-metapackage
|
+-MyEntity1.java
|
+-MyEntity1_.java

这是我的元模型:

    public static volatile SingularAttribute< MyEntity1, String> country;
public static volatile SingularAttribute< MyEntity1, String> city;
public static volatile SingularAttribute< MyEntity1, LocalDate> added;
public static volatile SingularAttribute< MyEntity1, String> address;
public static volatile SingularAttribute< MyEntity1, UserJPA> userJpa;
public static volatile SingularAttribute< MyEntity1, String> title;
public static volatile SingularAttribute< MyEntity1, Integer> size;
public static volatile SingularAttribute< MyEntity1, String> sizeUnit;
public static volatile SingularAttribute< MyEntity1, String> flags;// <<<<<<< this is a binary word of 8 bits
public static volatile SingularAttribute< MyEntity1, Long> id;

这是我的标准代码:

    CriteriaBuilder builder = entityManagerFactory.getCriteriaBuilder();

CriteriaQuery< MyEntity1> cquery = builder.createQuery( MyEntity1.class);
Root< MyEntity1> croot = cquery.from( MyEntity1.class);

cquery.select(croot);
Predicate pred = builder.isNotNull(croot.<String>get( MyEntity1_.flags));

if(RestPreconditions.checkString(model.getCity())) {
pred = builder.and(pred, builder.equal(croot.get( MyEntity1_.city), model.getCity()));
}
if(RestPreconditions.checkString(model.getCountry())) {
pred = builder.and(pred, builder.equal(croot.get( MyEntity1_.country), model.getCountry()));
}
if(RestPreconditions.checkString(model.getAddress())) {
pred = builder.and(pred, builder.equal(croot.get( MyEntity1_.address), model.getAddress()));
}

if(model.getMaxSize()!=null) {
pred = builder.and(pred, builder.lessThanOrEqualTo(croot.get( MyEntity1_.size), model.convertSizeToM2(model.getMaxSize())));
}
if(model.getMinSize()!=null) {
pred = builder.and(pred, builder.greaterThanOrEqualTo(croot.get( MyEntity1_.size), model.convertSizeToM2(model.getMinSize())));
}
// .flagsToBword() always returns something
String bword = model.flagsToBWord();
if(!bword.matches("0{8}")){
pred = builder.and(pred, builder.like(croot.<String>get( MyEntity1_.flags), bword.replaceAll("0", "_")));
}

return entityManagerFactory.createEntityManager()
.createQuery(cquery.where(pred))
.getResultList();

问题是我不断收到空列表作为响应,即使我知道数据库中有匹配的对象。

我做错了什么?

编辑:

例如,如果我想查找名为伦敦的城市的记录,这就是我发送的对象:

{
"city":"London"
}

我有一个与该城市连续的数据库记录。

此外,当我构造谓词时,开头的这行代码:

谓词 pred = builder.isNotNull(croot.get( MyEntity1_.flags));

是否只是为了启动合相;标志永远不会为空

最佳答案

您走在正确的道路上。您犯的唯一错误是您每次都覆盖 pred 的值。

If you want to chain where statements, you have to reassign pred value with itself + where clause.

Specification<MyEntity1> spec = Specification.where(
(root, cb, cq) -> cb.isNotNull(root.get(MyEntity1_.flags)));

if (RestPreconditions.checkString(model.getCity()))
spec = spec.and(
(root, cb, cq) -> cb.equal(root.get(MyEntity1_.city), model.getCity()))

return yourRepository.findAll(spec);

关于java - 条件查询返回[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56552230/

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