criteria-api

Really dynamic JPA CriteriaBuilder

好久不见. 提交于 2019-11-27 06:10:37
I need to create a "real" dynamic JPA CriteriaBuilder . I get an Map<String, String> with the statements. It looks like: name : John surname : Smith email : email@email.de ...more pairs possible Here is what i implement: CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<User> query = cb.createQuery(User.class); Root<User> userRoot = query.from(User.class); query.select(userRoot); List<Predicate> predicates = new ArrayList<Predicate>(); Iterator<String> column = statements.keySet().iterator(); while (column.hasNext()) { // get the pairs String colIndex = column.next(); String colValue

JPA Criteria select all instances with max values in their groups

我怕爱的太早我们不能终老 提交于 2019-11-27 03:36:24
问题 Is there a way to write with JPA 2 CriteriaBuilder the equivalent of the following query? select * from season s1 where end = ( select max(end) from season s2 where s1.contest_id=s2.contest_id ); In JPQL this query is: Select s1 from Season s1 where s1.end = ( select max(s2.end) from Season s2 where s1.contest=s2.contest ) 回答1: This should work, with contest being either a basic Integer property, or a ManyToOne property pointing to another non-basic Entity. EntityManger em; //to be injected

Criteria JPA 2 with 3 tables

廉价感情. 提交于 2019-11-27 00:45:08
问题 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

JPA - Criteria API and EmbeddedId

耗尽温柔 提交于 2019-11-26 23:01:00
问题 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 =

How to properly cast string to number with JPA2 Criteria API?

放肆的年华 提交于 2019-11-26 22:08:24
问题 I am trying to write a query with subselect where a string is cast to a long. I'm probably missing something? Query looks like: CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Task> query = cb.createQuery(Task.class); Root<Task> from = query.from(Task.class); Subquery<Long> subquery = query.subquery(Long.class); Root<EntityKeyword> fromKeyword = subquery.from(EntityKeyword.class); subquery.select(fromKeyword.get(EntityKeyword_.relatedToId).as(Long.class)); subquery.where(cb.like

JPA & Criteria API - Select only specific columns

≡放荡痞女 提交于 2019-11-26 21:53:29
I would like to select only specific columns (ex. SELECT a FROM b ). I have a generic DAO and what I came up with is: public List<T> getAll(boolean idAndVersionOnly) { CriteriaBuilder builder = manager.getCriteriaBuilder(); CriteriaQuery<T> criteria = builder.createQuery(entityClazz); Root<T> root = criteria.from(entityClazz); if (idAndVersionOnly) { criteria.select(root.get("ID").get("VERSION")); // HERE IS ERROR } else { criteria.select(root); } return manager.createQuery(criteria).getResultList(); } And the error is: The method select(Selection<? extends T>) in the type CriteriaQuery<T> is

How to query an M:N relationship with JPA2?

一世执手 提交于 2019-11-26 20:45:59
问题 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(

JPA/Hibernate Static Metamodel Attributes not Populated — NullPointerException

一曲冷凌霜 提交于 2019-11-26 20:42:53
I would like to use JPA2 Criteria API with metamodel objects, which seems to be pretty easy: ... Root<JPAAlbum> albm = cq.from(JPAAlbum.class); ... albm.get(JPAAlbum_.theme) ... ; but this Root.get always throws a NullPointerException . JPAAlbum_.theme was automatically generated by Hibernate and looks like public static volatile SingularAttribute<JPAAlbum, JPATheme> theme; but it's obviously never populated. Am I missing a step in the initialization of the framework ? EDIT: here is a snippet of how I use JPA and the metamodel when it's crashing: CriteriaBuilder cb = em.getCriteriaBuilder();

JPA CriteriaBuilder - How to use “IN” comparison operator

ぐ巨炮叔叔 提交于 2019-11-26 20:07:43
Can you please help me how to convert the following codes to using "in" operator of criteria builder? I need to filter by using list/array of usernames using "in". I also tried to search using JPA CriteriaBuilder - "in" method but cannot find good result. So I would really appreciate also if you can give me reference URLs for this topic. Thanks. Here is my codes: //usersList is a list of User that I need to put inside IN operator CriteriaBuilder builder = getJpaTemplate().getEntityManagerFactory().getCriteriaBuilder(); CriteriaQuery<ScheduleRequest> criteria = builder.createQuery

Compare Date entities in JPA Criteria API

孤者浪人 提交于 2019-11-26 16:13:03
问题 Using JPA 2 with EclipseLink implementation. I'm trying to build a dynamic query which should bring me some records persisted after a given date. CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Event> criteria = builder.createQuery(Event.class); Root<Event> root = criteria.from(Event.class); criteria.select(root); criteria.distinct(true); List<Predicate> predicates = new ArrayList<Predicate>(); //... if (dateLimit != null){ ParameterExpression<Date> param = builder.parameter