criteria-api

jpa criteria-api: join with subselect

一笑奈何 提交于 2019-12-23 07:36:13
问题 This query is used to retrieve last records in a one-to-many relationship (see SQL join: selecting the last records in a one-to-many relationship) SELECT p.* FROM customer c INNER JOIN ( SELECT customer_id, MAX(date) MaxDate FROM purchase GROUP BY customer_id ) MaxDates ON c.id = MaxDates.customer_id INNER JOIN purchase p ON MaxDates.customer_id = p.customer_id AND MaxDates.MaxDate = p.date; My question: How can I build this join with the subselect with the jpa criteria-api? Is it possible?

Select … in equivalent in JPA2 criteria

情到浓时终转凉″ 提交于 2019-12-22 08:12:13
问题 Is there any way to perform a query like the following using JPA2 criteria APIs? select a from b where a in (1, 2, 3, 4) There's a way to do that using plain Hibernate, but we can't find anything like that in JPA2. 回答1: Yes JPA 2 Critera supports returning a specific field from a entity and using a where clause which includes an in clause. I have included an example below which takes a JPQL and converts it to a similar JPA 2 Criteria-based option. JPQL: select b.a from B b where a in (1, 2, 3

Using @ElementCollection in CriteriaQuery (or Querying over the content of an @ElementCollection)

六眼飞鱼酱① 提交于 2019-12-22 05:24:15
问题 public enum ReportStatus { SUCCCEED, FAILED; } public class Work { @ElementCollection @Enumerated(EnumType.STRING) List<ReportStatus> reportStatuses; } Given the following structure, I'd like to perform a query to find all the work filtered by reportStatuses. It works fine with the following hql syntax : public List<Long> queryHQL() { final String query = "SELECT w.id FROM Work w JOIN w.reportStatuses s WHERE s in (:rs)"; final List<ReportStatus> reportStatuses = new ArrayList<ReportStatus>()

JPA CriteriaBuilder - sort by the number of associated entities in a one-to-many relationship

删除回忆录丶 提交于 2019-12-21 20:39:24
问题 I have two entities Customer and Order in a one-to-many relation. For each customer I need to count the number of associated orders and sort the results by this number. In a native postgres query it looks like this: select cust.id, count(order.id) from customers cust left outer join orders order on cust.id = order.customer_id where .... conditions ... group by cust.id order by count desc; But I must do this using CriteriaBuilder because this query is part of a larger piece of code that uses

How can I express joining to a grouped subquery using NHibernate?

怎甘沉沦 提交于 2019-12-21 13:06:16
问题 I'm trying to express a SQL query using NHibernate's Criteria API, and I'm running into difficulty because I'm thinking in a database-centric way while NHibernate is object-centric. SQL (works great): select outerT.id, outerT.col1, outerT.col2, outerT.col3 from tbl outerT inner join (select max(innerT.id) from tbl innerT group by innerT.col1) grpT on outerT.id = grpT.id Essentially, this is a self-join of a table against a subset of itself. I suppose I could try turning the self-join into a

Write HQL clause using Hibernate Criteria API

旧时模样 提交于 2019-12-21 05:05:06
问题 I want to write a method that returns a list of last added objects grouped by field 'serviceId'. The following HQL works, but I want to write this using Criteria API: FROM Notification WHERE date IN (SELECT MAX(date) FROM Notification GROUP BY serviceId) ORDER BY date ASC Something like this: Criteria crit = session.createCriteria(Notification.class); crit.add(Restrictions.in("date", <MAX dates>)); criteria.addOrder(Order.desc("date")); Thanks in advance. EDIT: Now I need a similar query that

Does Hibernate Criteria Api completely protect from SQL Injection

蓝咒 提交于 2019-12-21 04:14:25
问题 I am working with Hibernate to protect my website from SQL Injection. I heard that Hibernate Criteria API is more powerful than HQL. Does Hibernate Criteria Api completely protect from SQL Injection? 回答1: Yes, it does. Criteria API as well as query parameters in HQL or JPQL both escape the parameters and would not execute malicious SQL. The vulnerability is only exposed if you simply concatenate the parameters into your query. Then any malicious SQL becomes part of your query. EDIT The OWASP

JPA Criteria API: LEFT JOIN for optional relationships

天涯浪子 提交于 2019-12-21 03:59:08
问题 I'm using the Criteria API basically the first time. It's about abstracting queries for a generic builder: public TypedQuery<T> newQuery( Manager<?,T> manager ) { CriteriaBuilder builder = this.entityManager.getCriteriaBuilder(); Class<T> genericClass = ( Class<T> ) ( ( ParameterizedType ) manager.getClass().getGenericSuperclass() ).getActualTypeArguments()[1]; CriteriaQuery<T> criteriaQuery = builder.createQuery( genericClass ); Root<T> root = criteriaQuery.from( genericClass ); ... } The

CriteriaBuilder.and & CriteriaBuilder.or, how-to?

孤街浪徒 提交于 2019-12-21 03:37:41
问题 I'm trying to change the following HQL to use JPA Criteria: select distinct d from Department d left join fetch d.children c where d.parent is null and ( d.name like :term or c.name like :term ) order by d.name Department has a Set<Department> of children. Criteria: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<Department> c = cb.createQuery(Department.class); Root<Department> root = c.from(Department.class); root.fetch("children", JoinType.LEFT); Path<Department

Distinct results from Spring Data JPA Specification that uses join

空扰寡人 提交于 2019-12-21 03:19:22
问题 I have the following Specification that I use to query for any Contact entities that are tied to certain ManagedApplication entities. I pass in a Collection<Long> that contains the ids of the ManagedApplication entities that I am searching for. public static Specification<Contact> findByApp(final Collection<Long> appIds) { return new Specification<Contact>() { @Override public Predicate toPredicate(Root<Contact> root, CriteriaQuery<?> query, CriteriaBuilder cb) { final Predicate appPredicate