通过REST进行JPA规范子查询搜索

时间:2019-02-05 09:28:20

标签: java hibernate spring-boot jpa

我在以多对多关系和2类进行搜索时遇到问题 联接表。我想知道如何修正我的代码以及网址 请求执行以使用Spring Boot 1.5启动此搜索。

1)我想让所有的教授都带(student.names = john and Professor.namep = david)

学生

private Long ids;
private Long names;

教授

private Long idp;
private Long namep;
@ManyToMany(fetch = FetchType.LAZY, cascade = {CascadeType.DETACH,
        CascadeType.MERGE,
        CascadeType.REFRESH,
        CascadeType.PERSIST})
@JoinTable(name = "StudentProfessor",
joinColumns = { @JoinColumn(name = "idp") },
inverseJoinColumns = { @JoinColumn(name = "ids") })
private Set<Student> student = new HashSet<>();

实际上,我只能使用以下命令搜索所有具有相应名称的教授:

规格

public class ProfessorSpecification implements Specification<Professor> {

private Professor filter;

    public ProfessorSpecification(Product filter) {
        super();
        this.filter = filter;

    }
    public Predicate toPredicate(Root<Professor> root, CriteriaQuery<?> cq,
            CriteriaBuilder cb) {
        Predicate p = cb.conjunction();
        if (filter.getType() != null) {
            p.getExpressions()
                    .add(cb.equal(root.get("names"), filter.getNamep()));
        }
    return p;
    }

}

RESTCONTROLER

@GetMapping(path = "/search/professor")
public ResponseEntity<List<Professor>> searchProfessor(Professor searchedProfessor) {
    Specification<Professor> spec = new ProfessorSpecification(searchedProfessor);
    List<Professor> result = repository.findAll(spec);
    return ResponseEntity.ok(result);
}

但是我不知道如何针对这种情况进行子查询

2)我想知道要搜索的网址

我尝试过

@GetMapping(path = "/search/professor{p}/student{s}")

http://localhost:8080/search/professor?namep=john/student?names=david 没有成功

0 个答案:

没有答案