1

What is the best way for creating dynamic queries using Spring Data JPA?

I want to create custom projection of many tables joined together in many different ways (5-10) for performance reasons. Something simillar to criteria, and projections in previous versions of hibernate.

For example: I have 6 tables: A, B, C, D, E, F. A, B and C are always joined together (ABC) and create 'result'. Tables D, E, F are are joined to ABC only when user fills certain search fields. User might search by any of D,E,F column, or all of them at once.

I want to achieve projection like this: Result(A.id, A.name, B.code, C.description).

I don't want to use @Query annotation because there's a lot of different possibilites how these tables can be joined.

Amr Aly
  • 3,871
  • 2
  • 16
  • 33
straw
  • 33
  • 6
  • Show us what have you tried yet. The question in current form is broad and may attract opinionated answers. – Bond - Java Bond Apr 06 '17 at 07:27
  • Use a Specification http://stackoverflow.com/questions/40397347/jpa-fully-dynamic-and-multi-level-criteria-api/40397632#40397632 – Essex Boy Apr 06 '17 at 07:29
  • I was trying specification, cause it would be the best option for it, but unfortunately it didn't work. Now I'll give a shot to http://docs.spring.io/spring-data/jpa/docs/1.10.1.RELEASE/reference/html/#projections.remodelling-data And then http://stackoverflow.com/a/30366582/6651146 – straw Apr 06 '17 at 07:52

0 Answers0