0

I have 2 tables A and B
Table A and table B have 10 each attributes
I only want to return 2 and not all 10 attributes of each table
(I want to have an Index-Only-SQL-Accesspath on my DB-Server)

How to build a JPA-Query like this:
select a.name, a.street, b.city, b.country from ... ?

MAST
  • 31
  • 3
  • You need to use an interface projection for this see here https://www.baeldung.com/spring-data-jpa-projections or create a constructor in entity for those selected field and use the constructor in query like `select com.sch.Person(a.name, a.street, b.city, b.country) from...` – Eklavya Sep 01 '20 at 08:08
  • @Rono He didn't say that he uses spring data. It is not really necessary for a plain JPQL query to use spring data and it will most likely confuse the poster. – Tobias Liefke Sep 02 '20 at 08:13
  • @MAST A JPA query isn't about tables, but about entities. Give an example of the implementation of your entities. Did you already read some documentation about JPA or JPQL? You can start by clicking the JPA tag in your question and selecting "Learn more…" than. – Tobias Liefke Sep 02 '20 at 08:13

1 Answers1

0

I have found a solution: Hibernate CriteriaBuilder to join multiple tables

With this solution, I can only set the attributes that I need for my specific use cases and the generated sql query perfectly fits my database index -> Index-Only-SQL-DB-Accesspath to achieve a high performance query

(Normally, I don't need this and it's ok to retrieve all attributes of a JPA entity)

MAST
  • 31
  • 3