我有以下简单实体:
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
答案 0 :(得分:1)
这应该很简单
findByRecipientsId(Long id)