简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM