Reputation: 2526
I have the following relation:
@Entity
@Table(name = "User")
public class User {
@JsonIgnore
@ManyToMany
@JoinTable(
name = "user_group",
joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
inverseJoinColumns = {@JoinColumn(name = "group_id", referencedColumnName = "id")}
)
private List<Group> groups = new ArrayList<>();
.......
}
So I have User <--> Group many-to-many relation.
I need to fetch all users that do not have associations with groups at all - get all users
whose groups
list is empty.
Is there a way to fetch users like that with a usage of spring data?
Any help is appreciated,
Thanks
Upvotes: 1
Views: 645
Reputation: 786
If you want to fetch the users who don't have groups, then try the below method in UserRepository.
public interface UserRepository extends JpaRepository<User,Long> {
Optional<List<User>> findAllByGroupsEmpty();
}
By above method, spring data jpa will fetch the users who groups are empty.
Upvotes: 1