criteria-api

Using the 'case…when…then…else…end' construct in the 'having' clause in JPA criteria query

别来无恙 提交于 2019-11-29 11:51:11
The following criteria query calculates the average of rating of different groups of products. CriteriaBuilder criteriaBuilder=entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple>criteriaQuery=criteriaBuilder.createQuery(Tuple.class); Metamodel metamodel=entityManager.getMetamodel(); EntityType<Product>entityType=metamodel.entity(Product.class); Root<Product>root=criteriaQuery.from(entityType); SetJoin<Product, Rating> join = root.join(Product_.ratingSet, JoinType.LEFT); Expression<Number> quotExpression = criteriaBuilder.quot(criteriaBuilder.sum(join.get(Rating_.ratingNum)),

CASE statement in HQL or Criteria

你离开我真会死。 提交于 2019-11-29 07:21:12
derived from this question , is it possible to use HQL or Criteria for the following SQL statement: SELECT e.type, count(e), count(d), count (case when gender = 'male' then 1 else NULL end) AS NumberOfMaleEmployees from Department d JOIN d.employees e WHERE e.dead = 'maybe' GROUP BY e.type Although google comes up with a few hits that state HQL supports CASE statements, Hibernate 3.6.6 fails with a QuerySyntaxException: unexpected token: CASE when I create the query above on an EntityManager instance. How much of a bad idea is it, to create another query for every e.type to determine the

JPA2 Criteria-API: select… in (select from where)

懵懂的女人 提交于 2019-11-29 05:17:17
I have the following database model: A aId AB aId bId B bId status In a Spring data Specification, I want to return the instances of A when B.status is 'X'. The JPQL code is the following: select a from A a where a in (select ab.id.a from AB ab where ab.id.b.status= :status) These are the model classes: @Entity public class A { private Long aId; @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "id.a") private Set<AB> ab; } @Entity public class B { private Long bId; private String Status; @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "id.b")

How to create specification using JpaSpecificationExecutor by combining tables?

﹥>﹥吖頭↗ 提交于 2019-11-29 04:36:40
I am using JpaSpecificationExecutor for creating custom queries. How do I create a Specification for the following SQL? select * from employee e, address a where e.id=23415 and e.name="Deepak" and a.city="Leeds"; Java Class : public static Specification<Employee> searchEmployee(final Map<String,String> myMap) { return new Specification<Employee>(){ @Override public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> query, CriteriaBuilder cb) { //Need to query two tables Employee and Address } } Here is a test that works @Test public void test1() { repository.save(makeEmployee("billy",

JpaSpecificationExecutor JOIN + ORDER BY in Specification

跟風遠走 提交于 2019-11-29 04:07:54
I have a query using a JOIN and ORDER BY and want to use it within my repository using the Criteria Api. Here I found, how to wrap such a query into a CriteriaQuery ( Link ). CriteriaQuery<Pet> cq = cb.createQuery(Pet.class); Root<Pet> pet = cq.from(Pet.class); Join<Pet, Owner> owner = cq.join(Pet_.owners); cq.select(pet); cq.orderBy(cb.asc(owner.get(Owner_.lastName),owner.get(Owner_.firstName))); On the other side, I found some examples to use the Criteria Api in Combination with a JpaRepository ( example ). The Problem is that all methods in the repository expect a Specification: T findOne

Hibernate Criteria API - adding a criterion: string should be in collection

谁说胖子不能爱 提交于 2019-11-29 02:26:52
I have to following entity object @Entity public class Foobar { ... private List<String> uuids; ... } Now I'd like to make a criteria query which would fetch all Foobar pojos whose uuids list contains the string "abc123", I'm just not sure how to make the appropriate criterion. I assume you are using a version of Hibernate that implements JPA 2.0. Here's a JPA 2.0 solution that should work with any compliant implementation. Please annotate uuids with JPA's @ElementCollection annotation. Don't use Hibernate's @CollectionOfElements as mentioned in some of the other answer comments. The latter

JPA Named Queries vs Criteria API?

心不动则不痛 提交于 2019-11-29 00:38:30
问题 Is there a heuristic/best practice/ruleset for a decision between the Criteria API and NamedQuery ? My thoughts so far : Named queries are generally more readable. Criteria queries are more flexible. Both are precompiled. I tend to rely on using named queries as long as possible, then changing to criteria. But maybe the urge to "flexify" the query by using the criteria API is a hint to suboptimal design (i.e. separation of concerns)? Thank you 回答1: Named queries are more optimal (they are

How to use JPA Criteria API when joining many tables

时光毁灭记忆、已成空白 提交于 2019-11-28 23:39:05
This is the further question to this: How to use JPA Criteria API in JOIN CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); CriteriaQuery<Company> criteria = criteriaBuilder.createQuery( Company.class ); Root<Company> companyRoot = criteria.from( Company.class ); Join<Company,Product> products = companyRoot.join("dentist"); Join<Company, City> cityJoin = companyRoot.join("address.city");//Company->Address->City-city criteria.where(criteriaBuilder.equal(products.get("category"), "dentist"), criteriaBuilder.equal(cityJoin.get("city"),"Leeds")); A company has an address, inside the

How do I count the number of rows returned by subquery?

删除回忆录丶 提交于 2019-11-28 23:28:07
I want to do something like this: select count(*) from (select ...) (As it would be in SQL), but in JPA. Any ideas on how I would do it? pkainulainen This should do the trick (If you want to use JPA criteria API): CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<Long> query = cb.createQuery(Long.class); Root<Entity> root = query.from(Entity.class); //Selecting the count query.select(cb.count(root)); //Create your search criteria Criteria criteria = ... //Adding search criteria query.where(criteria); Long count = getEntityManager().createQuery(query).getSingleResult()

How to make a CriteriaBuilder join with a custom “on” condition?

こ雲淡風輕ζ 提交于 2019-11-28 23:10:57
I want make a query where I join 2 tables, using the CriteriaBuilder. In MySQL the query I'm trying to make would look like this: SELECT * FROM order LEFT JOIN item ON order.id = item.order_id AND item.type_id = 1 I want to get all orders and if they have an item of type #1, I want to join with this item. However, if no item of type #1 is found, I still want to get the order. I can't figure out how to make this with the CriteriaBuilder. All I know how to make is: CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Order> cq = cb.createQuery(Order.class); Root<Order> order = cq.from