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

2025-01-18 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

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report