Objective: To inquire the book information according to the published area of the book
Implementation steps:
1 Entity Configuration
One: Books Information book
Many: Regional information bookarea
Entity mapping, adding a collection of area in a one-way map book and setting the @JoinColumn (name= "BookID")
@OneToMany
You do not need to set a relationship in Bookarea
Writing query statements
Repository Inheritance Jpaspecificationexecutor
Override FindAll and implement the public predicate topredicate of the specification interface (root<book> Root, criteriaquery<?> query, Criteriabuilder Criteriabuilder) method
Associating other tables requires use to Join
join<book,bookarea> Areajoin = Root.join ("Bookarealist", jointype.left);
Ps:bookarealist as the Onetomany attribute in the book entity
3 depending on the region code query, you can select multiple region codes, we need to use the in statement to handle
In<object> in = criteriabuilder.in (Areajoin.get ("AreaCode"));
AreaCode is an attribute in a bookarea entity.
Multi-table queries need to be over-duplicated, using GROUP by
Query.groupby (Root.get ("id"));
Summary complete
Springboot JPA One-to-many correlation query, dynamic complex query de-duplication in statement use