criteria-api

Specfiy columns which should be considered for distinct calculation

孤街醉人 提交于 2021-02-18 12:18:12
问题 I'm using javax.persistence.criteria.CriteriaBuilder and javax.persistence.criteria.CriteriaQuery to select some entities. I now want to select only the entities that are unique which should be specified by a certain column. There is the method javax.persistence.criteria.CriteriaQuery#distinct which only returns unique entities. I would rather need something like CriteriaQuery<T> distinct(String... columnNames) Do you know how I can bake such a distinct in my JPA CriteriaQuery ? It seems to

Date comparison using the JPA criteria API

笑着哭i 提交于 2021-02-18 02:59:45
问题 I got a range date picker with two dates: start and end , where both can be empty. I would like to filter a table, where the entity has exact one date: date . So, here are some examples. I'd like to match. Imaging the date to match is the current date (17/07/2016). null - 17/07/2016 -> match 17/07/2016 - null -> match 17/07/2016 - 17/07/2016 -> match null - null -> match all Those are the edge cases in my opinion and the reason why I am struggling so much. Actually my code looks like:

how to call the function that was using mysql keywords as parameters by the criteria query?

纵然是瞬间 提交于 2021-02-11 14:24:00
问题 I am using mysql with jpa specification query. I want to know how can i call the function that was using mysql keywords as parameters. Here is the example: select * from schema3.countries order by convert(name using GBK); The convert method using the using and GBK keywords as paramters. I want to call the convert function by the criteria query. I tried the below but it does not working for me. Expression expression = join.get(Country_.NAME); Expression orderExpression = builder.function(

How to select just the foreign key value using Criteria Query?

被刻印的时光 ゝ 提交于 2021-02-08 13:53:32
问题 Suppose I have two entities as: @Entity public class A { @Id private int id; @ManyToOne private B b; //more attributes } @Entity public class B { @Id private int id; } So, the table for A is having a column as b_id as the foreign key. Now, I want to select just the b_id based on some criteria on other fields. How can I do this using criteria query? I tried doing following which throws IllegalArgumentException saying "Unable to locate Attribute with the given name [b_id] on this ManagedType [A

How to select just the foreign key value using Criteria Query?

ⅰ亾dé卋堺 提交于 2021-02-08 13:52:54
问题 Suppose I have two entities as: @Entity public class A { @Id private int id; @ManyToOne private B b; //more attributes } @Entity public class B { @Id private int id; } So, the table for A is having a column as b_id as the foreign key. Now, I want to select just the b_id based on some criteria on other fields. How can I do this using criteria query? I tried doing following which throws IllegalArgumentException saying "Unable to locate Attribute with the given name [b_id] on this ManagedType [A

Using the same predicates for two criteria queries

孤人 提交于 2021-01-29 14:52:39
问题 I want to run a pair of queries using the same array of Predicate : one to count the records, one to get a certain page of records. This seems like a pretty normal use case, to me, so there must be a good way to do it, but I have yet to find it. So this is the part that fetches entities: CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); EntityType<ENTITY> entityType = entityManager.getMetamodel().entity(FooEntity.class); CriteriaQuery<FooEntity> entityQuery =

How to perform a join fetch in JPA Criteria without unchecked casting?

邮差的信 提交于 2021-01-28 06:30:15
问题 I need to do a JOIN FETCH in JPA Criteria using a static metamodel, however I'm at a loss on how to do it without getting an unchecked exception warning. Suppose we have a Thing entity with a lazily-initialized Other entity inside it. I want to retrieve Things with fetched Others, where other.someField="someValue". This is roughly how I would do it: public List<Thing> getThings() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Thing> cq = cb.createQuery(Thing.class); Root root =

Subqueries in select in eclipselink

放肆的年华 提交于 2021-01-28 04:38:47
问题 According to EclipseLink documentation it supports subqueries in the select clause (even if JPA doesn't require it). When I try to use this feature in JPA Criteria API: myCriteriaQuery .multiselect(Arrays.asList(mySubquery, ...)) .where(...) there is an error: java.lang.ClassCastException: org.eclipse.persistence.internal.jpa.querydef.SubQueryImpl cannot be cast to org.eclipse.persistence.internal.jpa.querydef.SelectionImpl at org.eclipse.persistence.internal.jpa.querydef.CriteriaQueryImpl

using subqueries in jpa criteria api

被刻印的时光 ゝ 提交于 2020-11-24 20:01:35
问题 I'm studying JPA criteria api and my database contains Employee table. I am trying to find all the employees who are paid second highest salary. I was able to write JPQL successfully as follows. SELECT e FROM Employee e WHERE e.salary = (SELECT MAX(emp.salary) FROM Employee emp WHERE emp.salary < (SELECT MAX(employee.salary) FROM Employee employee) ) but now I am trying to convert it to criteria api and have tried following. CriteriaQuery<Employee> c = cb.createQuery(Employee.class); Root