criteria-api

Criteria JPA 2 with 3 tables

▼魔方 西西 提交于 2019-11-28 05:25:12
I'm trying to create a criteria to retrieve some objects from 3 tables (Associate, Update and Detail). A Detail has reference to Associate and Update, and an Update has reference to a list of Details. My objective is to retrieve a list of Updates that has at least a Detail with null value in a specified field, given an Associate id. In JPQL was easy to do but the client said that this must be coded with criteria. My JPQL was: public List<Update> getUpdates(long associateId) { TypedQuery<Update> query = em.createQuery("select distinct u from Update u, Detail dt, Associate a " + "where dt.update

CASE statement in HQL or Criteria

本秂侑毒 提交于 2019-11-28 00:49:08
问题 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

Using JPA Criteria Api and hibernate spatial 4 together

别等时光非礼了梦想. 提交于 2019-11-27 23:11:10
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. I recently work at the exact same problem. My solution is a own Predicate for the within-keyword. public class WithinPredicate extends AbstractSimplePredicate implements Serializable { private final Expression<Point> matchExpression;

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

浪子不回头ぞ 提交于 2019-11-27 22:43:51
问题 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;

How to create specification using JpaSpecificationExecutor by combining tables?

女生的网名这么多〃 提交于 2019-11-27 22:23: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

How to query an M:N relationship with JPA2?

时光总嘲笑我的痴心妄想 提交于 2019-11-27 21:40:42
I have an an object (BlogPost) that contains an M:N collection of elements (Tags). How to query for an object (BlogPost) where at least one it its Tags matches an element in a set of Tags (defined by the user) with JPA2 (Hibernate). findBlogPostWithAtLeastOneMatchingTag(Collection<Tag> tags){ ???? } My main problem is, that I actually need to compare two collections of tags: - the collection of tags of the BlogPost. - the collection I search for I tried Select p from Post p where p.tags in(:tags) but it does not work, as my post entities have more than just one tag. So what could I do instead?

JPA - Criteria API and EmbeddedId

三世轮回 提交于 2019-11-27 21:08:52
I want to use criteria to make the following query. I have an Entity with EmbeddedId defined: @Entity @Table(name="TB_INTERFASES") public class Interfase implements Serializable { @EmbeddedId private InterfaseId id; } @Embeddable public class InterfaseId implements Serializable { @Column(name="CLASE") private String clase; } And the criteria query that i am trying to do is: CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder(); CriteriaQuery<Interfase> criteriaQuery = criteriaBuilder.createQuery(Interfase.class); Root<Interfase> entity = criteriaQuery.from(Interfase.class);

JpaSpecificationExecutor JOIN + ORDER BY in Specification

别说谁变了你拦得住时间么 提交于 2019-11-27 18:10:28
问题 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

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

扶醉桌前 提交于 2019-11-27 16:44:15
问题 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. 回答1: 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.

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

非 Y 不嫁゛ 提交于 2019-11-27 16:06:57
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 parcelNumberId = parcelSearchDetailDO.getParcelNumberId(); if (parcelNumberId != null && !parcelNumberId