I've got this JPA entity
@Entity @Table(name = "Todos")
public class Todo {
@Id @GeneratedValue
private Long id;
...
@CollectionTable(name = "Doers", joinColumns = @JoinColumn(name = "id"))
@ElementCollection(targetClass = String.class, fetch = FetchType.EAGER)
@NotEmpty
private final Set<String> doers = new HashSet<>(); // emails
@Email
private String owner;
}
And I need my JpaRepository to fetch for me all todo instances when a particular doer is attached to them.
i.e. Iterable findAllByDoersContaining(String email) // collection scan
That would be the exact contrary of findByOwnerIn(Collection doers), I'd say. Is it possible to achieve that join?
Thanks for any tip.
So I ended up implementing my own query:
@Query(value = "SELECT * FROM Todos t WHERE EXISTS (SELECT d.ID FROM Doers d WHERE d.ID = t.ID AND d.DOERS = :email)",
nativeQuery = true)
Streamable<Todo> findAllByAssigneesContaining(@Param("email") String email);