Reputation: 447
I do have three database tables (MariaDB):
A:
+----+
| ID |[...]
+----+
B:
+----+
| ID |[...]
+----+
A_B (Crosstable)
+-----+-----+
| AID | BID |
+-----+-----+
Now I have an instance of entity A and would like to get a list using Spring Data JPA repositories:
public interface BRepository extends JPARepository<B, Long> {
List<B> bList = findByAId(Integer aId);
}
This solution does not work. What is the correct way to resolve that relation? The documentation only shows the very simple queries, i.e. querying by column value.
The entities themselves do work, I can store data correctly. Let me know, if I did not provide all necessary information.
Added information (relevant parts of entities):
Entity B:
[...]
@ManyToMany(mappedBy = "B")
List<A> aList;
[...]
Entity A:
[...]
@ManyToMany
@JoinTable(name = "A_B",
joinColumns = {
@JoinColumn(
name = "a_id",
referencedColumnName = "id"
)
},
inverseJoinColumns =
@JoinColumn(
name = "b_id",
referencedColumnName = "id"
)
)
private List<B> bList = new ArrayList<B>();
[...]
Upvotes: 1
Views: 1152
Reputation: 447
Okay, at the end it was much easier than I thought. I just had to call:
List<b> bList = aRepository.findOne(id).getBList();
Upvotes: 1