Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

java - Spring data jpa specification based on the postgres query

I want to create a spring jpa specification that would behave like this query:

SELECT * FROM files f WHERE f.status = :status AND f.file_id IN (SELECT initiations.file_id FROM initiations WHERE iban = :iban)

The first part I managed to create as that:

public Slice<File> findAll(FileRequest fileRequest) {
    return fileRepository.findAll(getSpecification(fileRequest), fileRequest.getPageable());
}


private Specification<File> getSpecification(FileRequest fileRequest) {
    return Specification.where(isOfStatus(fileRequest.getStatus()));
}


private Specification<File> isOfStatus(String status) {
    return (Specification<File>) (root, criteriaQuery, criteriaBuilder) -> {
        javax.persistence.criteria.Predicate statusEqual = criteriaBuilder.equal(root.get("status"), status);
        return criteriaBuilder.and(statusEqual);
    };
}

Now I'm stuck with the second part, I'm not sure how to create the part WHERE IN especially that it is a different SELECT from a different repository/table. So how should I make the Specification so it could create a list of values from initiationsRepository and then search for a match in it, as it is done in the query I've provided.

question from:https://stackoverflow.com/questions/66045666/spring-data-jpa-specification-based-on-the-postgres-query

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...