java - 使用 @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 方法名称
多谢你们 :)
解决方案
经过一段时间的环顾和尝试不同的事情......答案:
当您要查询 Table2 时,您需要将其添加到方法中:
findBymainIdAndTable2List_StatusInAndTable2List_personId
所以基本上添加列表名称,后跟下划线和该表中的列名称。如果有人想添加更多,请随意:D 这就是我让它工作的方式
推荐阅读
- matlab - 如何根据 MATLAB 中的某些条件将值添加到表的最后一列?
- android - 如何在 Room Dao 函数中更新实体的 TypeConverted 列
- css - CSS Grid 嵌套在 wrapper-div 或 body 元素中?
- node.js - 相当于 nodeJS express 中的 rails 控制台
- python - 无法使用 [dot]module 导入模块
- computer-vision - 使用深度学习特征的 SLAM 系统?
- javascript - 异步函数的返回语义是什么?
- mongodb - MongoDB - 查找 BinData 等于 UUID 值的位置
- amazon-s3 - 如何使用 Apache Kafka、Amazon Glue 和 Amazon S3 创建 Datalake?
- scikit-learn - 使用 Google Cloud ML Engine 和 XGBoost 调整超参数