简体   繁体   中英

Sprig Data derived method name with "parameter in collection attribute"

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.

ie 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);

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