criteria-api

Project an Entity from a relationship with Hibernate Criteria

回眸只為那壹抹淺笑 提交于 2019-12-01 01:56:42
I'm having trouble getting a list of related entity objects as the result of a criteria expression. I have two objects with a many to many relationship such that ObjectA <-> ObjectB where a single instance of ObjectA can be tied to a multiple instances of ObjectB, and an instance of ObjectB maybe tied to multiple instances of ObjectA. This relationship is stored in your typical join table, but for legacy reasons the object model is such that ObjectB has no direct knowledge of it's relationship to ObjectA. I'm trying to create a criteria expression to get all of the instances ObjectB that are

How to use CriteriaQuery SUM of custom operation on some cells?

十年热恋 提交于 2019-11-30 23:11:42
Consider you have table T, with fields A and B. With regular SQL, I could do this: SELECT SUM(A * (100.0 - B) / 100.0) AS D FROM T; And I would get exactly what I expect. However, I'm not sure how to do it with CriteriaQuery. I know how to do sum over 1 field, but not how to do sum over some math expression over multiple fields in a row. The CriteriaBuilder interface provides the following arithmetic functions: addition: sum(a, b) substraction: diff(a, b) multiplication: prod(a, b) division: quot(a, b) where a b parameters can be an expression and/or literal. As for the query, here is an

Hibernate Criteria API - how to order by collection size?

£可爱£侵袭症+ 提交于 2019-11-30 22:01:43
Assuming I have classes User and UserGroup. There is an optional 1-many group to user association and the association is mapped from both sides (the UserGroup side via a property called "members", which is a HashSet, the User side via property "group"). Using the Criteria API, how can I query for all groups, sorted by count of group members? (Edit) I should have pointed out when I asked this that pagination was being performed in the SQL, so the ordering should also be performed in the SQL if possible. Another option would be to use the @Formula annotation which is basically the equivalent of

Project an Entity from a relationship with Hibernate Criteria

泪湿孤枕 提交于 2019-11-30 21:20:37
问题 I'm having trouble getting a list of related entity objects as the result of a criteria expression. I have two objects with a many to many relationship such that ObjectA <-> ObjectB where a single instance of ObjectA can be tied to a multiple instances of ObjectB, and an instance of ObjectB maybe tied to multiple instances of ObjectA. This relationship is stored in your typical join table, but for legacy reasons the object model is such that ObjectB has no direct knowledge of it's

truncate/delete from given the entity class

妖精的绣舞 提交于 2019-11-30 18:16:10
I have my entity class available via a method. I'm trying to figure out, how via the JPA JPQL or Criteria API's I could issue a truncate or delete from. I think that the criteria api is more natural for working with classes, and truncate is a faster operation so these are prefered. This is what I put together so far, but not sure what to add/change about it. CriteriaBuilder cb = this._em().getCriteriaBuilder(); cb.createQuery( _entityClass() ).from( _entityClass() ); note: _entityClass returns MyEntity.class , I have no other references to MyEntity this is a more generalized implementation.

Criteria API: Fetch of a list returns repeated main entity

三世轮回 提交于 2019-11-30 13:21:27
问题 I have the following Entities; Ticket contains a set of 0,N WorkOrder: @Entity public class Ticket { ... @OneToMany(mappedBy="ticket", cascade = CascadeType.ALL, fetch = FetchType.LAZY) private List<WorkOrder> workOrders = null; ... } @Entity public class WorkOrder { ... @ManyToOne @JoinColumn(nullable = false) private Ticket ticket; } I am loading Tickets and fetching the attributes. All of the 0,1 attributes present no problem. For workOrders, I used this answer to get the following code.

JPA Criteria API IN expression Parameter list

谁说我不能喝 提交于 2019-11-30 13:11:38
问题 Is there a possibility to use a parameter list in Criteria API .in expression? I have something like this: List<Long> list = new ArrayList<Long>(); list.add((long)1); list.add((long)2); list.add((long)3); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Bewerbung> criteriaQuery = cb.createQuery(Bewerbung.class); Root<Bewerbung> bewerbung = criteriaQuery.from(Bewerbung.class); criteriaQuery.select(bewerbung).where( cb.in(bewerbung.get(Bewerbung_.bewerberNummer)).value(list); return

Using Projections in JPA 2

断了今生、忘了曾经 提交于 2019-11-30 12:43:32
问题 I need to convert a Hibernate criteria query like the following curList = session.createCriteria(Islem.class) .createAlias("workingDay", "d") .setProjection(Projections.sum("amount")) .add(Restrictions.eq("currency", CURRENCY)) .add(Restrictions.eq("product", product)) .add(Restrictions.ne("status", INACTIVE)) .add(Restrictions.eq("d.status", ACTIVE)) .getResultList(); However in JPA (2) I have no idea how to implement the projection - in this case - the sum. It's odd that Hibernate and JPA

How to join unrelated entities with the JPA Criteria API

情到浓时终转凉″ 提交于 2019-11-30 11:12:59
Two database tables have a foreign key relationship. They are mapped to two entities A and B by JPA, but the join columns are manually removed from the entities, so in JPA world classes A and B are not related and you cannot navigate from one to the other through a field/property. Using the JPA Criteria API, is it possible to create a query which joins the two tables? All examples I found on internet uses the join column to achieve the goal, but, as stated above, it was removed from the code because most time I'm not interested in the relationship between A and B and I'm afraid about possible

Criteria API: Fetch of a list returns repeated main entity

孤者浪人 提交于 2019-11-30 09:31:37
I have the following Entities; Ticket contains a set of 0,N WorkOrder: @Entity public class Ticket { ... @OneToMany(mappedBy="ticket", cascade = CascadeType.ALL, fetch = FetchType.LAZY) private List<WorkOrder> workOrders = null; ... } @Entity public class WorkOrder { ... @ManyToOne @JoinColumn(nullable = false) private Ticket ticket; } I am loading Tickets and fetching the attributes. All of the 0,1 attributes present no problem. For workOrders, I used this answer to get the following code. CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder(); CriteriaQuery<Ticket>