简体   繁体   English

Spring JPA规范:在父类中搜索参数

[英]Spring JPA Specifications: searching for parameters in a parent class

I can't seem to figure out how to make the parent class params be available for specification queries. 我似乎无法弄清楚如何使父类参数可用于规范查询。 If I query using the RoleDAO parameter name then I get a result, but if I try to search by a value of id of BaseDAO present in the DB, then nothing is returned. 如果我使用RoleDAO参数name进行查询, RoleDAO得到结果,但是如果尝试通过数据库中存在的BaseDAOid值进行搜索,则不会返回任何内容。

If on the other hand I move the id param into the RoleDAO then search is working correctly. 另一方面,如果我将id参数移到RoleDAO则搜索工作正常。

Entity looks like this: 实体看起来像这样:

@EqualsAndHashCode(callSuper = true)
@Data
@Entity
@Table(name = "user_role", indexes = {
        @Index(name = "id_index", columnList = "id"),
        @Index(name = "name_index", columnList = "name"),
})
public class RoleDAO extends BaseDAO {

    @NotEmpty(message = "{error.not-empty}")
    @Column
    private String name;

}

BaseDAO: BaseDAO:

@MappedSuperclass
@Data
public class BaseDAO implements Serializable {

    private static final long serialVersionUID = 1;

    @Id
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    @GeneratedValue(generator = "uuid")
    @Column(name = "id", unique = true, nullable = false)
    private String id;

    @NotNull(message = "{error.not-null}")
    @Column(name = "created")
    private LocalDateTime created;

    @NotEmpty(message = "{error.not-empty}")
    @Size(max = 200, message = "{error.max}")
    @Column(name = "created_by")
    private String createdBy;

    @Column(name = "modified")
    private LocalDateTime modified;

    @Size(max = 200, message = "{error.max}")
    @Column(name = "modified_by")
    private String modifiedBy;

    @PrePersist
    public void prePersist() {
        id = UUID.randomUUID().toString();
        created = LocalDateTime.now();
    }

    @PreUpdate
    public void preUpdate() {
        modified = LocalDateTime.now();
    }
}

Specification: 规格:

public class Specifications<T> {

    public Specification<T> containsTextInAttributes(String text, List<String> attributes) {
        if (!text.contains("%")) {
            text = "%" + text + "%";
        }
        String finalText = text;

        return (root, query, builder) -> builder.or(root.getModel().getDeclaredSingularAttributes().stream()
                .filter(a -> attributes.contains(a.getName()))
                .map(a -> builder.like(root.get(a.getName()), finalText))
                .toArray(Predicate[]::new));
    }
}

Then there is a repository with a method: 然后是带有方法的存储库:

List<RoleDAO> findAll(Specification<RoleDAO> spec);

And how it is called in a service: 以及如何在服务中调用它:

var roles = repository.findAll(
                Specification.where(new Specifications<RoleDAO>().containsTextInAttributes(searchTerm, Arrays.asList(ID, NAME)))
        );

Solution was very simple: 解决方案非常简单:

public class Specifications<T> {

    public Specification<T> containsTextInAttributes(String text, List<String> attributes) {
        if (!text.contains("%")) {
            text = "%" + text + "%";
        }
        String finalText = text;

        return (root, query, builder) -> builder.or(root.getModel().getSingularAttributes().stream()
                .filter(a -> attributes.contains(a.getName()))
                .map(a -> builder.like(root.get(a.getName()), finalText))
                .toArray(Predicate[]::new));
    }
}

Notice the getSingularAttributes() call change. 请注意getSingularAttributes()调用更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM