您好,登錄后才能下訂單哦!
本篇內容主要講解“JPA CriteriaBuilder子查詢方式是什么”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“JPA CriteriaBuilder子查詢方式是什么”吧!
給自己做個備忘
/** 檢索關鍵字 */ if (keyWord != null && !keyWord.trim().equals("")) { // 創建子查詢對象 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 + "%")); // 給讀者稍微解釋一下,也給自己留個記憶 // 這里的意圖是將關鍵做兩個模糊檢索,一個是在本表中檢索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語法,所以是
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);
到此,相信大家對“JPA CriteriaBuilder子查詢方式是什么”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。