在spring jparepository中加入多个表

Man*_*esh 3 spring jpql spring-data spring-data-jpa

我试图通过加入来获取记录.我是spring jparepository的新手.我知道每个实体(表)都有单独的存储库,当我实现时,我需要定义主键的实体和数据类型.

任何人都可以建议我如何通过加入两个表来获取记录.

我有两个回购如下:

public interface AEntityRepository extends JpaRepository<AEntity, Integer>

public interface BEntityRepository extends JpaRepository<BEntity, Integer>
Run Code Online (Sandbox Code Playgroud)

我想加入两个实体(AEntity,BEntity).我知道我可以使用以下内容进行自定义查询:

@Query("SELECT ****** FROM AEntity ae")
AEntity findCustomrRecords();
Run Code Online (Sandbox Code Playgroud)

但是我可以使用join编写相同类型的查询(连接查询).我是否需要一个单独的存储库来实现其他类.

谁能请帮忙.

我正在使用mysql.

Jen*_*der 5

我知道每个实体都有单独的存储库(表)

这是一个非常普遍的误解.您不希望每个实体都有一个存储库,但是每个聚合根都有.见http://static.olivergierke.de/lectures/ddd-and-spring/

关于您手头的具体问题:在存储库界面中创建自定义方法并使用JPQL对其进行注释应该可以解决问题.所以你会得到类似的东西:

@Query("select a from BEntity b join b.a a where b.foo = :foo")
AEntity getAllFooishAs(String foo);
Run Code Online (Sandbox Code Playgroud)

您可以使用JPQL在查询中提供的任何联接语法.