简体   繁体   中英

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.

If on the other hand I move the id param into the RoleDAO then search is working correctly.

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:

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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