0

I have three entities in my Spring Boot JPA project.

A_Ent


| A_field1 | A_field2 | A_field3 |


B_Ent


| B_field1 | B_field2 | B_field3 |


C_Ent


| C_field1 | C_field2 | C_field3 |


I want to create queries with innerjoins in some of the repositories.

fx:

SELECT A_field1, B_field2 C_field3 FROM A_Ent AE
inner join B_Ent BE on (AE.field3 = BE.field3)
inner join C_Ent CE on (AE.field2 = CE.field2)
WHERE CE.field3 = 1

how can i add specific inner joins selects to my spring boot jpa repositories (if possible) ?

Anders Pedersen
  • 2,255
  • 4
  • 25
  • 49
  • 3
    Possible duplicate of [How To Define a JPA Repository Query with a Join](http://stackoverflow.com/questions/13154818/how-to-define-a-jpa-repository-query-with-a-join) – Strelok Feb 15 '17 at 14:39
  • 2
    clearly JPQL joins across relations, so without posting the entities and their relations, there is nothing to comment on – Neil Stockton Feb 15 '17 at 15:21

1 Answers1

-1

I think you need to use the old style of join in your JPQL query (i am assuming you have to join by non-primary/foreign keys):

select AE.A_field1, BE.B_field2 CE.C_field3 
from A_Ent AE, B_Ent BE, C_Ent CE 
where (AE.field3 = BE.field3)
   and (AE.field2 = CE.field2)
   and CE.field3 = 1

You have to remember that this will be a projection so you will get an Object[] or List<Object[]>, depending whether you use uniqueResult() or list() methods on the query respectively

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63