Reputation: 1372
I do have a repository
public interface GroupRepository extends JpaRepository<Group, Integer> {
}
The Group does have a list of items
private List<Item> items;
Each Item has a position property
private int position;
How can I retrieve a group by knowing the position of an item present in one of the lists?
public class Item extends PersistedBean{
private int position;
private Group group;
@Column(name = "Position")
public int getPosition() {
return position;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "GroupId", referencedColumnName = "Id")
public Group getGroup() {
return group;
}
}
public class Group extends PersistedBean {
private int position;
private List<Item> items;
@Column(name = "Position")
public int getPosition() {
return position;
}
@OneToMany(cascade = CascadeType.ALL, mappedBy = "group", orphanRemoval = false)
public List<Item> getItems() {
return items;
}
}
Upvotes: 3
Views: 1705
Reputation: 2219
You could add query to your JpaRepository<Group, Integer>
repository, something like should work:
@Query("SELECT g FROM Group g " +
"JOIN g.items i ON i.position IN :p")
List<Group> getGroupsWhenPositionMatchAnyRelatedItem(@Param("p") Integer p);
Upvotes: 2