criteria-api

Some basic questions on Criteria from JPA 2.0

谁都会走 提交于 2019-12-03 13:55:26
问题 I discovered JPA 2.0 Criteria API today and want to learn it. Just went through some examples and try to do a hands on. I have a table fruit with columns: id, name, color, size, taste. The regular stuff: EntityManagerFactory emf = Persistence.createEntityManagerFactory("fruitManager"); EntityManager em = emf.createEntityManager(); //get the criteria builder CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Fruit> c = cb.createQuery(Fruit.class); How will the following query get

spring-data subquery within a Specification

≯℡__Kan透↙ 提交于 2019-12-03 12:23:51
问题 Spring-data, Oliver Gierke's excellent library, has something called a Specification (org.springframework.data.jpa.domain.Specification). With it you can generate several predicates to narrow your criteria for searching. Can someone provide an example of using a Subquery from within a Specification? I have an object graph and the search criteria can get pretty hairy. I would like to use a Specification to help with the narrowing of the search, but I need to use a Subquery to see if some of

How to do a distinct count in JPA critera API?

笑着哭i 提交于 2019-12-03 11:50:31
I would like to do this but with the criteria API instead: select count(distinct e) from Event e, IN(e.userAccessPermissions) p where p.principal = :principal and p.permission in (:permissions) Any ideas? You can use countDistinct on CriteriaBuilder criteriaQuery.select(criteriaBuilder.countDistinct(entityRoot)) Like this? Criteria crit = session.createCriteria(Event.class): crit.createAlias("userAccessPermissions", "p"); crit.add(Restrictions.eq("p.principal", principal); crit.add(Restrictions.in("p.permission", permissions); crit.setProjection(Projections.countDistinct("id")); Use c.distinct

When to use the select clause in the JPA criteria API?

回眸只為那壹抹淺笑 提交于 2019-12-03 10:50:17
问题 Without using CriteriaQuery#select() : public List<Address> getAddressOfManager(String designation, String name, String orderByColumn) { Boolean ascending = false; CriteriaBuilder cb = emanager.getCriteriaBuilder(); CriteriaQuery<Address> cq = cb.createQuery(Address.class); Root<Address> root = cq.from(Address.class); //cq.select(root); <------------- Join<Address, Employee> employeeAddress = root.join(Address_.employee); Join<Employee,Project> employeeProject = employeeAddress.join(Employee_

JPA 2.0: count for arbitrary CriteriaQuery?

岁酱吖の 提交于 2019-12-03 09:28:21
问题 I am trying to implement the following convenience method: /** * Counts the number of results of a search. * @param criteria The criteria for the query. * @return The number of results of the query. */ public int findCountByCriteria(CriteriaQuery<?> criteria); In Hibernate, this is done by criteria.setProjection(Projections.rowCount()); What is the equivalent to the above in JPA? I found numerous simple count examples, but none of them made use of a CriteriaQuery whose row count should be

EntityManager injection - NullPointerException

一笑奈何 提交于 2019-12-03 08:43:36
In my Spring+JPA/Hibernate+Wicket app, I have a QueryBuilder bean that I want to use in one of my DAOs which generates a typed query with the help of Criteria API: @Service(value="inboxQueryBuilder") public class InboxQueryBuilder { @PersistenceContext EntityManager em; CriteriaBuilder cb; public InboxQueryBuilder() { cb = em.getCriteriaBuilder(); } public TypedQuery<App> getQueryForApps(AppSearchObject aso) { ... } ... } However, when I run the app, I get a null pointer exception for line: cb = em.getCriteriaBuilder(); i.e. the EntityManager doesn't get injected. Do you know why? Also, is

jpa lazy fetch entities over multiple levels with criteria api

蓝咒 提交于 2019-12-03 06:56:47
I am using JPA2 with it's Criteria API to select my entities from the database. The implementation is OpenJPA on WebSphere Application Server. All my entities are modeled with Fetchtype=Lazy. I select an entity with some criteria from the database and want to load all nested data from sub-tables at once. If I have a datamodel where table A is joined oneToMany to table B, I can use a Fetch-clause in my criteria query: CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<A> cq = cb.createQuery(A.class); Root<A> root = cq.from(A.class); Fetch<A,B> fetch = root.fetch(A_

Grails Criteria projections on joined table

时间秒杀一切 提交于 2019-12-03 06:18:56
问题 I have an issue with grails criteria builder, I want to do a projection on a column that is on a table that is in one-to-many relation to parent table example: Car.createCriteria() { projections { property('name') property('wheels.name')// ???? } join 'wheels' //or wheels {} ??? } or something similar exist? I think it is basic propblem with aliases 回答1: I am assuming the following domain classes: class Car { String name static hasMany = [wheels : Wheel] } class Wheel { String name static

What does an underscore concatenated to a class name mean?

自作多情 提交于 2019-12-03 05:34:11
问题 I was reading the "Dynamic, typesafe queries in JPA 2.0" article and stumbled upon this example: EntityManager em = ... CriteriaBuilder qb = em.getCriteriaBuilder(); CriteriaQuery<Person> c = qb.createQuery(Person.class); Root<Person> p = c.from(Person.class); Predicate condition = qb.gt(p.get(Person_.age), 20); // ^^ --- this one c.where(condition); TypedQuery<Person> q = em.createQuery(c); List<Person> result = q.getResultList(); I was wondering, what exactly does the underscore here mean?

Some basic questions on Criteria from JPA 2.0

别来无恙 提交于 2019-12-03 03:50:28
I discovered JPA 2.0 Criteria API today and want to learn it. Just went through some examples and try to do a hands on. I have a table fruit with columns: id, name, color, size, taste. The regular stuff: EntityManagerFactory emf = Persistence.createEntityManagerFactory("fruitManager"); EntityManager em = emf.createEntityManager(); //get the criteria builder CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Fruit> c = cb.createQuery(Fruit.class); How will the following query get built using criteria: select id, name, color where name like 'XY%' and color='orange' I.e. how to: fetch