criteria-api

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

瘦欲@ 提交于 2020-01-02 06:25: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)

Criteria API: filter by class type

佐手、 提交于 2019-12-30 06:58:26
问题 I'm relativley new to relational databases and I have some problems concerning the creation of queries. First I want to explain the situation shortly. I have several entity classes. All of them extend AbstractEntity or EntityProperty . So entities can have properties and properties have owning entities, so there is a bidirectional relation. Now let's say ConcreteEntity extends AbstractEntity and I want to create queries like this: Get all entities of type ConcreteEntity which has at least on

SELECT DISTINCT + ORDER BY in JPA 2 Criteria API

回眸只為那壹抹淺笑 提交于 2019-12-30 06:09:43
问题 I've a class Lawsuit , that contains a List<Hearing> , each one with a Date attribute. I need to select all the Lawsuit s ordered by the date of their Hearing s I've a CriteriaQuery like CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Lawsuit> cq = cb.createQuery(Lawsuit.class); Root<Lawsuit> root = cq.from(Lawsuit.class); I use distinct to flatten the results: cq.select(root).distinct(true); I then join Lawsuit with Hearing Join<Lawsuit, Hearing> hearing = root.join("hearings",

SELECT DISTINCT + ORDER BY in JPA 2 Criteria API

ⅰ亾dé卋堺 提交于 2019-12-30 06:09:05
问题 I've a class Lawsuit , that contains a List<Hearing> , each one with a Date attribute. I need to select all the Lawsuit s ordered by the date of their Hearing s I've a CriteriaQuery like CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Lawsuit> cq = cb.createQuery(Lawsuit.class); Root<Lawsuit> root = cq.from(Lawsuit.class); I use distinct to flatten the results: cq.select(root).distinct(true); I then join Lawsuit with Hearing Join<Lawsuit, Hearing> hearing = root.join("hearings",

SELECT DISTINCT + ORDER BY in JPA 2 Criteria API

夙愿已清 提交于 2019-12-30 06:09:03
问题 I've a class Lawsuit , that contains a List<Hearing> , each one with a Date attribute. I need to select all the Lawsuit s ordered by the date of their Hearing s I've a CriteriaQuery like CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Lawsuit> cq = cb.createQuery(Lawsuit.class); Root<Lawsuit> root = cq.from(Lawsuit.class); I use distinct to flatten the results: cq.select(root).distinct(true); I then join Lawsuit with Hearing Join<Lawsuit, Hearing> hearing = root.join("hearings",

How to use JPA Criteria API when joining many tables

孤街浪徒 提交于 2019-12-29 04:43:06
问题 This is the further question to this: How to use JPA Criteria API in JOIN CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); CriteriaQuery<Company> criteria = criteriaBuilder.createQuery( Company.class ); Root<Company> companyRoot = criteria.from( Company.class ); Join<Company,Product> products = companyRoot.join("dentist"); Join<Company, City> cityJoin = companyRoot.join("address.city");//Company->Address->City-city criteria.where(criteriaBuilder.equal(products.get("category"),

Java Criteria API join self referencing entity

眉间皱痕 提交于 2019-12-25 11:28:08
问题 I'm trying to do a nested join on a self referencing entity, please bear with me, it will make sense :) Thing.java class Thing { Integer something; Person owner; } Person.java class Person { String name; Person parent; } Note: A Person with parent=null means that they are a parent, and cannot have a parent. It's an alien world! :P Query: Root<Thing> root = query.from(Thing.class); Join<Thing, Person> ownerJoin = root.join(Thing_.owner); Join<Person, Person> parentJoin = ownerJoin.join(Person_

Java Criteria API join self referencing entity

自古美人都是妖i 提交于 2019-12-25 11:27:04
问题 I'm trying to do a nested join on a self referencing entity, please bear with me, it will make sense :) Thing.java class Thing { Integer something; Person owner; } Person.java class Person { String name; Person parent; } Note: A Person with parent=null means that they are a parent, and cannot have a parent. It's an alien world! :P Query: Root<Thing> root = query.from(Thing.class); Join<Thing, Person> ownerJoin = root.join(Thing_.owner); Join<Person, Person> parentJoin = ownerJoin.join(Person_

JPA How to get the number of parameter markers used in Criteria API?

隐身守侯 提交于 2019-12-25 03:55:05
问题 How do I get the number of parameter markers used in Criteria API? I am using Criteria API to create SQL statement with IN keyword that have many parameter markers. CriteriaBuilder cb = ... ... CriteriaBuilder.In<String> in = cb.in(...); ... for (...) { in.value(...); } I started getting SQLException "Prepared or callable statement has more than 2000 parameter markers." Is there a way to get the actual number of parameter markers used so far? I could count them by myself, but it is error

How to retrive objects from one to many using HQL or Criteria API

吃可爱长大的小学妹 提交于 2019-12-25 03:43:41
问题 Started to learn hibernate with examples. I have written a class "Team" that has one to many relationship with (a Collection of) players. I need to get all the teams where player name is "X" (X is not the primary key of the Player table) where player "Y" is in I think I have to get this done by using Criteria API or the HQL. Can someone tell how I can achieve it. 回答1: Thsi will work for OneToMany relationship, ie 1 Team can Have many Players , and every no Player will be mapped to more than 1