我遇到了一个“ Spring Data JPA”查询,由于我需要多次传递相同的userId,因此很难看。
我的查询是:
Page<CompanyEntity> findByClients_User_IdOrOwners_User_IdOrRelations_User_IdOrProjectManagers_User_IdOrSpecialists_User_Id(Long userId1, Long userId2, Long userId3, Long userId4, Long userId5, Pageable page);
根据建议,我应该重写此查询以使用JPQL。
但是我仍然坚持如何从集合中获取用户对象
编辑:
@Query("SELECT company FROM CompanyEntity company JOIN company.owners owners JOIN company.clients clients JOIN company.projectManagers managers JOIN company.specialists specialists JOIN company.relations relations")
答案 0 :(得分:1)
@Query("SELECT company FROM CompanyEntity company JOIN company.owners owners JOIN company.clients clients WHERE :userId IN owners.id OR :userId IN clients.id")
Page<CompanyEntity> find(@param("userId") Long userId, Pageable pageable)
(对于其他关联,依此类推)应该可以解决问题。