首页 > 解决方案 > 使用 @Query 的 2 表查询的 Spring JPA 方法名称

问题描述

已编辑

好奇...我有一个自定义的 Spring JPA 查询,我不确定如何编写。

我正在扩展 PagingAndSortingRepository

@Query: select * from Table1 tb1 JOIN Table2 tb2 on tb1.id = tb2.tb1_id where tb2.personId = :personId and tb1.mainId=:mainId and tb2.status in (:statusList)

我不确定如何为此创建方法名称,因为它一直给我一个错误,说它在 Table1 中找不到状态。

我想出类似的东西: public Page findByMainIdAndStatusInAndPersonId(@Param("mainId") Integer mainId, ..........); 会工作,但它告诉我它找不到状态。这是可以理解的,因为状态位于我尝试加入的 Table2 对象中。

**Table1**
id
column1
column2
mainId
List<Table2> table2List

**Table2**
id
table1_id
status
person_id

表 1 和表 2 通过表 2 的 table_id 列链接。但是在 Table1 JPA 存储库中,我需要根据 Table2 中的标准获取所有 Table1。

我检查了“属性表达式”,但我不知道如何编写 jpa 方法名称

多谢你们 :)

标签: javaspringhibernatespring-data-jpa

解决方案


经过一段时间的环顾和尝试不同的事情......答案:

当您要查询 Table2 时,您需要将其添加到方法中:

findBymainIdAndTable2List_StatusInAndTable2List_personId

所以基本上添加列表名称,后跟下划线和该表中的列名称。如果有人想添加更多,请随意:D 这就是我让它工作的方式


推荐阅读