Good day! There are two entities, for example, the user and documents related many to many. I want to select all documents, the current user (through the object does not suit, because there are additional parameters such as type, status ...). I can not understand how to correctly write a request. I use Spring Data.

@Query("select c from Doc c , User u where c.type=:type and u.id = :userId and c.status =:statusId") List<Edoc> findAllByUserAndType (@Param("type") int type,@Param("userId") long userId ,@Param("statusId") int statusId ); 

The first entity:

 public class Edoc { @ManyToMany @JoinTable(name = "Edocs_users", schema = "edocs", joinColumns = @JoinColumn(name = "edoc_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id")) private Set<User> users = new HashSet<User>(); } 

The second entity:

 public class User { @ManyToMany(mappedBy = "users" ,fetch = FetchType.EAGER) private Set<Edoc> edocs = new HashSet<Edoc>(); } 

    1 answer 1

    In Spring Data, JPA repositories support the creation of queries by method names. The number of parameters in the query does not matter, and the query can not write. In your case:

     public interface EdocRepository extends JpaRepository<Edoc, Long> { List<Edoc> findAllByUserIdAndTypeAndStatusId(long userId, int type, int statusId); } 

    The list of keywords is described in the documentation http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-creation