清单(JpaRepository)中包含“ id”的默认Spring Data查询

时间:2018-09-11 21:39:25

标签: java spring spring-data-jpa spring-data nativequery

我有以下简单实体:

Suggestion.java:

@Entity
public class Suggestion {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @OneToOne
    private Employee author;

    @OneToMany
    @JoinColumn(name = "recipients_id")
    private List<Employee> recipients;
}

和Employee.java:

@Entity
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    private String name;
}

我想在控制器中返回所有suggestions,其中id列表中仅包含一个雇员的recipients

是否可以避免自定义查询(本机查询)?

我尝试过:

findByRecipientsContains(id)或

findByRecipientsContaining(id)

但没有运气...


编辑:

在存储库中使用时:

Optional<List<Suggestion>> findByRecipientsIn(Long id);

也没有Optional并且在控制器中:

    @GetMapping("/employees/{id}/suggestions")
    @ResponseStatus(HttpStatus.OK)
    public List<Suggestion> getSuggestionsByRecipient(@PathVariable("id") Long id) {
        return suggestionRepository.findByRecipientsIn(id).get();
    }

我得到如下异常:

Parameter value element [1] did not match expected type [com.herokuapp.erpmesbackend.erpmesbackend.employees.Employee (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value element [1] did not match expected type [com.herokuapp.erpmesbackend.erpmesbackend.employees.Employee (n/a)]] with root cause