criteria-api

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

时间秒杀一切 提交于 2019-12-18 07:03:18
问题 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

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

强颜欢笑 提交于 2019-12-18 02:27:42
问题 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? 回答1: 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

JPA Criteria API: Multiple condition on LEFT JOIN

有些话、适合烂在心里 提交于 2019-12-17 19:24:08
问题 I have a join reference like following for which the first join expression is constructed by the JPA API automatically. CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple> c = cb.createTupleQuery(); Root<Demand> demands = c.from(Demand.class); Join<Demand, Metadata> joinMetadata = demands.join("metadatas", JoinType.LEFT); but, I would like to add an aditional condition to my joinMetadata like Metadata.type="AFFECTATION_KEY" but I don't know how. Many thanks for any

JPA/Criteria API - Like & equal problem

梦想的初衷 提交于 2019-12-17 17:35:25
问题 I'm trying to use Criteria API in my new project: public List<Employee> findEmps(String name) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Employee> c = cb.createQuery(Employee.class); Root<Employee> emp = c.from(Employee.class); c.select(emp); c.distinct(emp); List<Predicate> criteria = new ArrayList<Predicate>(); if (name != null) { ParameterExpression<String> p = cb.parameter(String.class, "name"); criteria.add(cb.equal(emp.get("name"), p)); } /* ... */ if (criteria.size()

Using JPA Criteria Api and hibernate spatial 4 together

跟風遠走 提交于 2019-12-17 16:31:12
问题 Given the query example here: http://www.hibernatespatial.org/tutorial-hs4.html Query query = em.createQuery("select e from Event e where within(e.location, :filter) = true", Event.class); query.setParameter("filter", filter); Is it possible to rewrite the query using jpa 2 criteria api?( I am unsure how i should deal with the within(e.location, :filter) part. 回答1: I recently work at the exact same problem. My solution is a own Predicate for the within-keyword. public class WithinPredicate

Using JPA 2.0 Criteria API and cast causes generated JPQL to fail in Hibernate

允我心安 提交于 2019-12-17 14:47:05
问题 I am a first time user of the new JPA 2.0 Criteria API and I 'm running into a problem when I need to cast a number field to String to compare it with a String parameter. Reason is that I want to search for partial numbers, so I use a 'like' on the CriteriaBuilder. Here's a code sample: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<ParcelDO> cq = cb.createQuery(ParcelDO.class); Root<ParcelDO> parcelDO = cq.from(ParcelDO.class); cq.select(parcelDO); String

JPA Criteria API with multiple parameters

末鹿安然 提交于 2019-12-17 10:22:20
问题 I need to make a search method that uses the JPA Criteria API with multiple parameters. Now the problem is that not every parameter is required. So some could be null, and they shouldn't be included in the query. I've tried this with the CriteriaBuilder but I couldn't see how to make it work. With the Hibernate Criteria API this is fairly easy. Just create the criteria and then add Restrictions. Criteria criteria = session.createCriteria(someClass.class); if(someClass.getName() != null) {

criteria api set predicate in where clause for MapJoin

谁说我不能喝 提交于 2019-12-13 21:13:00
问题 I'm having some trouble with setting the where clause of the following query: CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Configuration> cq = cb.createQuery(Configuration.class); Root<Configuration> conf = cq.from(Configuration.class); MapJoin<Configuration, String, Component> mapJoin = conf.join(Configuration_.components, JoinType.LEFT); cq.where(cb.and(cb.like(mapJoin.key(), "%abc%"), cb.like(mapJoin.value().get(Component_.displayName), "%def%"))); cq.select(pc); I am

Criteria Query select join

倾然丶 夕夏残阳落幕 提交于 2019-12-13 07:14:03
问题 I need help to convert this JPQL query in Criteria Query: SELECT u.documentList FROM Unit u WHERE u.id = :id this is what i have tried: CriteriaQuery<Document> query = builder.createQuery(Document.class); Root<Unit> root = query.from(Unit.class); Join<Unit, Document> join = root.join(Unit_.documentList); query.select(join); query.where(builder.equal(root.get(AbstractEntity_.id), entity.getId())); executing this query result in a complex SQL query that returns an empty list. IMO this should

Criteria eclipselink join

那年仲夏 提交于 2019-12-13 02:56:48
问题 I can't understand how do a join in eclipse link with criteria. This my entity: public class A implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @Column(name = "id") private Long id; @Column(name = "value") private String value; @OneToMany(mappedBy = "aid") private Collection<B> bCollection; } public class B implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @Column(name = "id")