1

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

plkpiotr
  • 345
  • 2
  • 9
  • 23
  • pass the whole employee not just the id. – mavriksc Sep 11 '18 at 21:54
  • 1
    after looking at the docs... i think `contains` is a string operation and `In` or `IsIn` is a set operation. maybe `findSuggestionByEmployeeInRecipients(Employee e)` – mavriksc Sep 11 '18 at 22:02
  • @mavriksc Thanks, I didn't describe whole of my architecture but I use requests with id, so I am forced to implement "id" – plkpiotr Sep 11 '18 at 22:08
  • @mavriksc But thank you for your reply and clue :) – plkpiotr Sep 11 '18 at 22:10
  • 1
    I'm almost sure you can do this: findByRecipientsIn(List idsList). I don't have my personal computer now but I remember I had to do something similar before. – Martin Sep 11 '18 at 22:15
  • Thank you @Martin, I would like to find suggestions for only one employee's id, so maybe the solution is close :) – plkpiotr Sep 11 '18 at 22:21
  • So, I edited post to be more precise... – plkpiotr Sep 11 '18 at 22:28
  • 1
    You have only one id, but the recipients attribute is a list, so I think the correct thing would be that the service that calls the repository creates a list with a single element. So that you can reuse that function for when you have more ids in the future. I don't think you can do it otherwise unless you use a @Query annotation. – Martin Sep 11 '18 at 22:55
  • Yeah, I noticed the observation too :) There is nothing in the way of trying this tomorrow... Thanks a lot ;) – plkpiotr Sep 11 '18 at 23:05

1 Answers1

4

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

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348