简体   繁体   中英

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.

I'm extending 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)

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.

I figured something like: 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.

**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. however in the Table1 JPA repository, I need to fetch all of Table1 based on criteria in Table2.

I checked "property expressions" but I'm not catching how to write the jpa method name

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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