简体   繁体   English

Spring Data JPA查询+交叉表

[英]Spring Data JPA Query + Crosstable

I do have three database tables (MariaDB): 我有三个数据库表(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: 现在,我有一个实体A的实例,想使用Spring Data JPA存储库获取一个列表:

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, ie 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>();
[...]

Okay, at the end it was much easier than I thought. 好的,最后,这比我想象的要容易得多。 I just had to call: 我只需要打电话给:

List<b> bList = aRepository.findOne(id).getBList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM