I have the simple entities as follows:
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;
}
and Employee.java:
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
}
I would like to return in the controller all of the suggestions
which contain only one employee's id
in recipients
list.
It is possible to avoid custom query (native query)?
I tried:
findByRecipientsContains(id) or
findByRecipientsContaining(id)
but no luck...
EDIT:
When I used in repository:
Optional<List<Suggestion>> findByRecipientsIn(Long id);
also without Optional
and in controller:
@GetMapping("/employees/{id}/suggestions")
@ResponseStatus(HttpStatus.OK)
public List<Suggestion> getSuggestionsByRecipient(@PathVariable("id") Long id) {
return suggestionRepository.findByRecipientsIn(id).get();
}
I get the exception as follows:
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
That should work with simply
findByRecipientsId(Long id)
Compare to this test case: https://github.com/spring-projects/spring-data-jpa/blob/688becd2b7129b853cd0deaf6bde3b50d9d8ce50/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java#L604