Here is my USER entity and USER_GROUP is a relation table with GROUP. How to fetch all Users Where groupid = ?
@Entity
@Table(name="USER")
public class User implements Serializable {
@Id Long userId;
@ManyToOne
@JoinTable(name = "USER_GROUP",
joinColumns = @JoinColumn(name = "userid"),
inverseJoinColumns = @JoinColumn(name = "groupid"))
private List<Group> groups;
}
I believe you are using UserRepository
as well.
In your UserRepository
you can write a method to fetch all such data.
public interface UserRepository extends JpaRepository<User, Long>{
@Query(value="Select u from User u inner join u.groups group where group.groupid = :groupId ")
List<User> findAllByGroupId(@Param("groupId") long groupId)
}
This should work. However i have not tested it.
Note Use DISTINCT
to select only unique User
objects.