您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“JPA CriteriaBuilder子查詢方式是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“JPA CriteriaBuilder子查詢方式是什么”吧!
給自己做個(gè)備忘
/** 檢索關(guān)鍵字 */ if (keyWord != null && !keyWord.trim().equals("")) { // 創(chuàng)建子查詢對(duì)象 Subquery<Store> subquery = criteriaQuery.subquery(Store.class); Root<Store> root2 = subquery.from(Store.class); subquery.select(root2.<Store> get("id")); subquery.where(criteriaBuilder.like(root2.<String> get("name"), "%" + keyWord + "%")); // 給讀者稍微解釋一下,也給自己留個(gè)記憶 // 這里的意圖是將關(guān)鍵做兩個(gè)模糊檢索,一個(gè)是在本表中檢索title字段 // 就是criteriaBuilder.like(root.<String> get("title"), "%" + keyWord + "%") // 二是將字段與子表中的檢索出來的id與本表中store字段做匹配 // criteriaBuilder.in(root.get("store")).value(subquery)) // 至于id怎么檢索出來的,用子查詢模糊檢索出來的,參照上面的子查詢 restrictions = criteriaBuilder.and(restrictions,criteriaBuilder.or(criteriaBuilder.like(root.<String> get("title"), "%" + keyWord + "%"),criteriaBuilder.in(root.get("store")).value(subquery))); }
final CriteriaBuilder cb = entityManager.getCriteriaBuilder(); final CriteriaQuery<Person> cq = cb.createQuery(Person.class); final Root<Person> root = cq.from(Person.class); cq.select(root); final Subquery<Integer> subquery = cq.subquery(Integer.class); final Root<Person> 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)
自身所需NotIn語(yǔ)法,所以是
Subquery<String> manageUserSubquery = query.subquery(String.class); Root<RelationUserManagePlateDomain> relationUserManagePlateDomainRoot = manageUserSubquery.from(RelationUserManagePlateDomain.class); Join<RelationUserManagePlateDomain, UserDomain> relationUserManagePlateDomainUserDomainJoin = relationUserManagePlateDomainRoot.join(RelationUserManagePlateDomain_.user, JoinType.INNER); Subquery<String> subquery = manageUserSubquery.select(relationUserManagePlateDomainUserDomainJoin.get(UserDomain_.id)); Predicate predicate1 = criteriaBuilder.and(criteriaBuilder.not(criteriaBuilder.in(root.get(UserDomain_.id)).value(manageUserSubquery))); predicate.getExpressions().add(predicate1);
到此,相信大家對(duì)“JPA CriteriaBuilder子查詢方式是什么”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。