简体   繁体   English

使用@Query的2表查询的Spring JPA方法名称

[英]Spring JPA Method name for a 2 table query using @Query

EDITTED 已编辑

Curious... I have a custom Spring JPA query which I'm not sure how to write. 很好...我有一个自定义的Spring JPA查询,我不确定该如何编写。

I'm extending PagingAndSortingRepository 我正在扩展PagingAndSortingRepository

The @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) @Query:select * from Table1 tb1 JOIN Table2 tb2 on tb1.id = tb2.tb1_id其中tb2.personId =:personId和tb1.mainId =:mainId和tb2.status在(:statusList)中

I'm not sure how to create the method name for this as it keeps giving me an error saying it can't find status in the Table1. 我不确定如何为此创建方法名称,因为它一直给我一个错误,提示它无法在Table1中找到状态。

I figured something like: public Page findByMainIdAndStatusInAndPersonId(@Param("mainId") Integer mainId, ..........); 我想像这样的东西:public Page findByMainIdAndStatusInAndPersonId(@Param(“ mainId”)Integer mainId,..........); would work but it's telling me it can't find status. 可以,但是告诉我找不到状态。 Which is understandable since status is in the Table2 object which I'm trying to join on. 这是可以理解的,因为状态在我要加入的Table2对象中。

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

**Table2**
id
table1_id
status
person_id

table 1 and 2 are linked via table2's table_id column. 表1和2通过table2的table_id列链接。 however in the Table1 JPA repository, I need to fetch all of Table1 based on criteria in Table2. 但是,在Table1 JPA存储库中,我需要根据Table2中的条件获取所有Table1。

I checked "property expressions" but I'm not catching how to write the jpa method name 我检查了“属性表达式”,但没有掌握如何编写jpa方法名称

Thanks guys :) 多谢你们 :)

After a while of looking around and trying different things... ANSWER: 环顾四周并尝试不同的方法后...答案:

When you want to query on Table2, you need to add it to the method as: 当您要在Table2上进行查询时,需要将其添加到方法中,如下所示:

findBymainIdAndTable2List_StatusInAndTable2List_personId

So essentially add the list name followed by underscore and the column name in that table. 因此,基本上在该表中添加列表名称,后跟下划线和列名称。 If anyone wants to add more, def feel free :D This is how I got it to work 如果有人想添加更多内容,那么def自由:D这就是我如何使其工作的方式

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

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