Answer the question
In order to leave comments, you need to log in
How to build a dynamic query in JPA?
Hello.
There is a helloworld project on Spring + Hibernate.
There are three entities: Offer, Car and Brand. Described in hbm.xml.
They are related as follows: Offer.car and Car.brand.
I need to get all Offers by Brand.id.
In SQL it looks like this: select * from offers, cars, brands where offers..car_id = cars.id and cars.brand_id = brands.id and brands.id = n;
I want to file a dynamic query build if only the entities at the ends of the chain are known. For example, when in the chain E1.e2 -> E2.e3 -> . . . -> En.em -> Em only E1 and Em are known, and the length of the chain of related entities is not known. Hence the question: is there any ready-made elegant way to get a list of entities? Or maybe you can immediately get the ResultSet?
You need to avoid things like:
1. SQL hardcoding via the @Query annotation.
2. use of string constants with table names.
3. filing a separate findAll () method in the service for each request.
I use JpaSpecificationExecutor.findAll(Specification)) and classes that extend CrudRepository, JpaSpecificationExecutor.
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question