获得完全符合指定条件的子记录

时间:2019-06-17 09:29:41

标签: java specifications

我有2张桌子,如下所示:

public class Type {

    @Column(name = "name")
    private String name;

    @NotAudited
    @OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.REMOVE, CascadeType.PERSIST}, orphanRemoval = true)
    @JoinColumn(name = "type_code_id", referencedColumnName = "id")
    private List<TypeList> typeLists;
}
public class TypeList {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "type_id", referencedColumnName = "id")
    private Type type;
    @Column(name = "description")
    private String description;
    @Column(name = "category")
    private String category;

}

并有一个规范:

private Specification<Type> searchConditions(final Request request) {
        return  (root, cq, cb) -> {
            Predicate predicate = cb.isNotNull(root.get("id"));

            if (!StringUtils.isEmpty(request.getPattern())) {
                final String pattern = like(request.getPattern());
                final Join<Object, Object> link = root.join("TypeList");
                predicate = cb.and(predicate, cb.or(
                        cb.like(root.get("name"), pattern),
                        cb.like(link.get("description"), pattern),
                        cb.like(link.get("category"), pattern)
                ));
            }
            return predicate;
        };
    }

当投放模式为“工作”时,它将返回

"types" : [ {
      "name" : "1",
      "id" : "1",
      "typeLists" : [ {
        "id" : "2c0f68ae-7190-11e9-a5a2-12ff1f270001",
        "category" : "Work1",
        "description" : "Work2"
      }, {
        "id" : "2c0f68ae-7190-11e9-a5a2-12ff1f270002",
        "category" : "test",
        "description" : "test"
      } ]
    }, {
      "name" : "2",
      "id" : "2",
      "typeLists" : [ {
        "id" : "773e4245-bcf4-4b51-b3a3-a06558e0be35",
        "category" : "Work",
        "description" : "Work"
      }]
    }
]

我不要下面的记录:

"id" : "2c0f68ae-7190-11e9-a5a2-12ff1f270002",
        "category" : "test",
        "description" : "test"

我的预期结果只是想接收与我的模式完全匹配的记录。

如何忽略它?如果您有任何解决方案,请提出建议。

0 个答案:

没有答案