Springboot JPA One-to-many correlation query, dynamic complex query de-duplication in statement use

Source: Internet
Author: User

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

    1. 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.

    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.