criteria-api

Using the JPA Criteria API, can you do a fetch join that results in only one join?

好久不见. 提交于 2019-11-26 14:06:17
问题 Using JPA 2.0. It seems that by default (no explicit fetch), @OneToOne(fetch = FetchType.EAGER) fields are fetched in 1 + N queries, where N is the number of results containing an Entity that defines the relationship to a distinct related entity. Using the Criteria API, I might try to avoid that as follows: CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<MyEntity> query = builder.createQuery(MyEntity.class); Root<MyEntity> root = query.from(MyEntity.class); Join

JPA Criteria API - How to add JOIN clause (as general sentence as possible)

泄露秘密 提交于 2019-11-26 12:55:40
问题 I am trying to construct queries dynamically, and my next target is add JOIN clauses (I don\'t know how can I use the API). By now, for example, this code work for me : ... Class baseClass; ... CriteriaBuilder cb = JpaHandle.get().getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(this.baseClass); Root entity_ = cq.from(this.baseClass); Predicate restrictions = null; ... restrictions = cb.conjunction(); restrictions = cb.and(restrictions, entity_.get(\"id\").in(this.listId)); ... cq

Really dynamic JPA CriteriaBuilder

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 11:53:12
问题 I need to create a \"real\" dynamic JPA CriteriaBuilder . I get an Map<String, String> with the statements. It looks like: name : John surname : Smith email : email@email.de ...more pairs possible Here is what i implement: CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<User> query = cb.createQuery(User.class); Root<User> userRoot = query.from(User.class); query.select(userRoot); List<Predicate> predicates = new ArrayList<Predicate>(); Iterator<String> column = statements.keySet()

JPA/Hibernate Static Metamodel Attributes not Populated — NullPointerException

不问归期 提交于 2019-11-26 09:02:33
问题 I would like to use JPA2 Criteria API with metamodel objects, which seems to be pretty easy: ... Root<JPAAlbum> albm = cq.from(JPAAlbum.class); ... albm.get(JPAAlbum_.theme) ... ; but this Root.get always throws a NullPointerException . JPAAlbum_.theme was automatically generated by Hibernate and looks like public static volatile SingularAttribute<JPAAlbum, JPATheme> theme; but it\'s obviously never populated. Am I missing a step in the initialization of the framework ? EDIT: here is a

JPA CriteriaBuilder - How to use “IN” comparison operator

折月煮酒 提交于 2019-11-26 09:00:08
问题 Can you please help me how to convert the following codes to using \"in\" operator of criteria builder? I need to filter by using list/array of usernames using \"in\". I also tried to search using JPA CriteriaBuilder - \"in\" method but cannot find good result. So I would really appreciate also if you can give me reference URLs for this topic. Thanks. Here is my codes: //usersList is a list of User that I need to put inside IN operator CriteriaBuilder builder = getJpaTemplate()

JPA & Criteria API - Select only specific columns

柔情痞子 提交于 2019-11-26 08:04:24
问题 I would like to select only specific columns (ex. SELECT a FROM b ). I have a generic DAO and what I came up with is: public List<T> getAll(boolean idAndVersionOnly) { CriteriaBuilder builder = manager.getCriteriaBuilder(); CriteriaQuery<T> criteria = builder.createQuery(entityClazz); Root<T> root = criteria.from(entityClazz); if (idAndVersionOnly) { criteria.select(root.get(\"ID\").get(\"VERSION\")); // HERE IS ERROR } else { criteria.select(root); } return manager.createQuery(criteria)

JPA 2.0, Criteria API, Subqueries, In Expressions

那年仲夏 提交于 2019-11-26 00:59:05
问题 I have tried to write a query statement with a subquery and an IN expression for many times. But I have never succeeded. I always get the exception, \" Syntax error near keyword \'IN\' \", the query statement was build like this, SELECT t0.ID, t0.NAME FROM EMPLOYEE t0 WHERE IN (SELECT ? FROM PROJECT t2, EMPLOYEE t1 WHERE ((t2.NAME = ?) AND (t1.ID = t2.project))) I know the word before \'IN\' lose. Have you ever written such a query? Any suggestion? 回答1: Below is the pseudo-code for using sub

JPA 2.0, Criteria API, Subqueries, In Expressions

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-25 21:34:57
I have tried to write a query statement with a subquery and an IN expression for many times. But I have never succeeded. I always get the exception, " Syntax error near keyword 'IN' ", the query statement was build like this, SELECT t0.ID, t0.NAME FROM EMPLOYEE t0 WHERE IN (SELECT ? FROM PROJECT t2, EMPLOYEE t1 WHERE ((t2.NAME = ?) AND (t1.ID = t2.project))) I know the word before 'IN' lose. Have you ever written such a query? Any suggestion? Below is the pseudo-code for using sub-query using Criteria API. CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery