List (JpaRepository) 中包含 "id" 的默认 Spring 数据查询

Default Spring Data query with containing "id" in List (JpaRepository)

我有如下简单的实体:

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;
}

我想 return 控制器中的所有 suggestions 其中包含 只有一个 员工的 idrecipients 列表。

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

我试过了:

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

这应该可以简单地使用

findByRecipientsId(Long id)

与此测试用例比较:https://github.com/spring-projects/spring-data-jpa/blob/688becd2b7129b853cd0deaf6bde3b50d9d8ce50/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java#L604