criteria-api

CriteriaBuilder: join one-to-many with ON clause

三世轮回 提交于 2019-12-06 01:32:15
Suppose you the following OneToMany relationships: School->Student->ScientificWork . Now you want to select all Schools that have Student with name 'John' and his scientific job is called 'Black Holes'. I do it like the following, but for some reason it retrurns me all possible schools. public static Specification<School> spec() { return (root, query, cb) -> { final SetJoin<School, Student> studs = root.joinSet("students", JoinType.LEFT); final SetJoin<Student, ScientificWork> works = root.joinSet("works", JoinType.LEFT); return cb.and( cb.equal(studs.get(Student_.name), 'John'), cb.equal

Jpa Criteria API count

风流意气都作罢 提交于 2019-12-06 00:16:23
I've been trying to get total rows count and to do so, I've used JPA Criteria API but it throws an error at Long count = em.createQuery(sc).getSingleResult(); line and saying java.lang.IllegalStateException: No criteria query roots were specified . I've done some research but couldn't narrow the problem. Here's my code snippet; @PersistenceContext public EntityManager em; ...... public Page<UserDTO> findByCriteria(String filters, Pageable pageable) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<UserDTO> cq = cb.createQuery(UserDTO.class); Root<UserDTO> iRoot = cq.from(UserDTO

How to build a dynamic query which add number of days to date and compare that date with another date using criteria API?

时光总嘲笑我的痴心妄想 提交于 2019-12-05 17:42:14
I've got a A table, and a B table. Both JPA entity , A and B classes has joda.time.datetime Persistent Field say retentionDate and lastmodifiedDate respectively. A has one more Persistent Field of type int says days . Now I would like to add number of a.days into a.retentionDate and then compare it with b.lastmodifiedDate using JPA criteria API . A Entity class @Entity @Table(name = "A") @Data @JsonInclude(JsonInclude.Include.NON_NULL) @JsonIgnoreProperties(ignoreUnknown = true) @EqualsAndHashCode(of = "id", callSuper = false) public class A extends AbstractBaseEntity { @Id @GeneratedValue

Can I use table-valued function as query source in NHibernate?

我们两清 提交于 2019-12-05 16:35:32
I have one question to ask you, dear community, as you may have guessed. So. I want NHibernate to filter the results of a query basing on the evaluation of table-valued sql function. Possible SQL query generated by NHibernate may look similar to the following: SELECT [whatever] FROM [whatever] INNER JOIN dbo.FnMyTableValuedFunction() as MyAlias ON [whatever].FirstDesiredKey = MyAlias.FirstDesiredKey AND [whatever].SecondDesiredKey = MyAlias.SecondDesiredKey Or it can be written this way: SELECT [whatever] FROM [whatever] WHERE EXISTS( SELECT 1 FROM dbo.FnMyTableValuedFunction() AS MyAlias

Hibernate Criteria — return records where column is distinct

穿精又带淫゛_ 提交于 2019-12-05 12:52:17
Sample database table: ID = 1, msgFrom = 'Hello', foobar = 'meh' ID = 2, msgFrom = 'Goodbye', foobar = 'comments' ID = 3, msgFrom = 'Hello', foobar = 'response' Sample desired output (generated by hibernate query): ID = 1, msgFrom = 'Hello', foobar = 'meh' ID = 2, msgFrom = 'Goodbye', foobar = 'comments' In the above example, the third record would be excluded from the results since the msgFrom column is the same. Let's say the Java/Hibernate class is called Message. I would like the results to be returned as a list of Message objects (or Objects that can be cast to Message, anyway). I want to

Select … in equivalent in JPA2 criteria

≯℡__Kan透↙ 提交于 2019-12-05 11:50:27
Is there any way to perform a query like the following using JPA2 criteria APIs? select a from b where a in (1, 2, 3, 4) There's a way to do that using plain Hibernate, but we can't find anything like that in JPA2. Yes JPA 2 Critera supports returning a specific field from a entity and using a where clause which includes an in clause. I have included an example below which takes a JPQL and converts it to a similar JPA 2 Criteria-based option. JPQL: select b.a from B b where a in (1, 2, 3, 4) Criteria: CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); // assuming a is an

Using @ElementCollection in CriteriaQuery (or Querying over the content of an @ElementCollection)

可紊 提交于 2019-12-05 07:49:19
public enum ReportStatus { SUCCCEED, FAILED; } public class Work { @ElementCollection @Enumerated(EnumType.STRING) List<ReportStatus> reportStatuses; } Given the following structure, I'd like to perform a query to find all the work filtered by reportStatuses. It works fine with the following hql syntax : public List<Long> queryHQL() { final String query = "SELECT w.id FROM Work w JOIN w.reportStatuses s WHERE s in (:rs)"; final List<ReportStatus> reportStatuses = new ArrayList<ReportStatus>(); reportStatuses.add(ReportStatus.FAILED); return this.entityManager.createQuery(query).setParameter(

Subquery in where clause with CriteriaQuery

最后都变了- 提交于 2019-12-05 07:10:33
Can anybody give me some hints on how to put that kind of subquery in a CriteriaQuery ? (I'm using JPA 2.0 - Hibernate 4.x ) SELECT a, b, c FROM tableA WHERE a = (SELECT d FROM tableB WHERE tableB.id = 3) - the second select will always get a single result or null. Try something like the following example to create a subquery: CriteriaQuery<Object[]> cq = cb.createQuery(Object[].class); Root tableA = cq.from(TableA.class); Subquery<String> sq = cq.subquery(TableB.class); Root tableB = cq.from(TableB.class); sq.select(tableB.get("d")); sq.where(cb.equal(tableB.get("id"), 3)); cq.multiselect( cb

If/Case statement in JPA Criteria Builder

[亡魂溺海] 提交于 2019-12-04 23:59:11
问题 I want to build up a query which search dates on different entites. My structure is: Contract has date (non nullable) Employee has date (non nullable) Employee may have a contract id (nullable) If a Employee has a contract I want to retrieve the contract date. If an employee does not have a contract then I want to return the Employee date. My code so far is: if (inputDate!= null) { ParameterExpression<Date> exp = criteriaBuilder.parameter(Date.class, "inputDate"); criteria.add(criteriaBuilder

How to convert SQL Query using CriteriaQuery

泪湿孤枕 提交于 2019-12-04 23:14:27
I want to convert my sql query from SQL to CriteriaQuery, I have this sql query: 1) SELECT * FROM table AS t WHERE id = (SELECT MAX(id) FROM table AS t WHERE t.element_id = 354 AND (name <> 'foo' OR (name = 'bar' AND event = 'foo'))); 2) SELECT tr1.* FROM table AS tr1 INNER JOIN (SELECT MAX(id) AS id FROM table AS tr WHERE tr.element_id = 354 AND (name <> 'foo' OR (name = 'bar' AND event = 'foo'))) AS tr2 ON tr1.id = tr2.id; How is the best way to do this ? public Predicate createsubqueryDateGreaterThanTest(CriteriaBuilder cb, Root<? extends Entity> root, Date inputDate){ // create the outer