Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the subquery method of JPA Criteria Builder

2026-02-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

The main content of this article is to explain "what is the sub-query method of JPA criteria Builder". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "JPA criteria Builder sub-query method is what" it!

JPA criteria Builder subquery

Make yourself a memo.

/ * * search keywords * / if (keyWord! = null & &! keyWord.trim (). Equals (")) {/ / create a subquery object Subquery subquery = criteriaQuery.subquery (Store.class); Root root2 = subquery.from (Store.class); subquery.select (root2. Get ("id"); subquery.where (criteriaBuilder.like (root2. Get ("name"), "%" + keyWord + "%"); / / explain a little to the reader and keep a memory for yourself / / the intention here is to do two fuzzy searches of the key, one is to search the title field / / that is criteriaBuilder.like (root. Get ("title"), "%" + keyWord + "%") / / second, match the field with the retrieved id in the subtable with the store field in this table / / criteriaBuilder.in (root.get ("store")) .value (subquery) / / as to how the id is retrieved vaguely by subquery, refer to the above subquery restrictions = criteriaBuilder.and (criteriaBuilder.like (root. Get ("title"), "%" + keyWord + "%"), criteriaBuilder.in (root.get ("store")) .value (subquery));} Jpa adds where conditional NotIN subquery final CriteriaBuilder cb = entityManager.getCriteriaBuilder (); final CriteriaQuery cq = cb.createQuery (Person.class); final Root root = cq.from (Person.class); cq.select (root); final Subquery subquery = cq.subquery (Integer.class); final Root validityIDSQ = subquery.from (Person.class) Subquery.select (cb.max (validityIDSQ.get (Person_.validityID); subquery.groupBy (validityIDSQ.get (Person_.personID)); cq.where (cb.in (root.get (Person_.validityID)) .value (subquery)) / / select person0_.id as id1_0_, person0_.personID as personID2_0_, person0_.validityID as validity3_0_, person0_.value as value4_0_from person person0_where person0_.validityID in (select max (person1_.validityID) from person person1_ group by person1_.personID) will be generated

The NotIn syntax you need, so it is

Subquery manageUserSubquery = query.subquery (String.class); Root relationUserManagePlateDomainRoot = manageUserSubquery.from (RelationUserManagePlateDomain.class); Join relationUserManagePlateDomainUserDomainJoin = relationUserManagePlateDomainRoot.join (RelationUserManagePlateDomain_.user, JoinType.INNER); Subquery subquery = manageUserSubquery.select (relationUserManagePlateDomainUserDomainJoin.get (UserDomain_.id)); Predicate predicate1 = criteriaBuilder.and (criteriaBuilder.in (root.get (UserDomain_.id)) .value (manageUserSubquery) Predicate.getExpressions (). Add (predicate1); at this point, I believe you have a deeper understanding of "what is the sub-query method of JPA criteria Builder". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

  • How to realize simple package pop-up window by WeChat Mini Programs

    Today, I would like to talk to you about how WeChat Mini Programs realizes the simple packaging pop-up window, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can gain something according to this article. 1. Create component folder 2. Write component content

    12
    Report