criteria-api

JPA 2 criteria API: How to select values from various joined tables without using Metamodel?

喜夏-厌秋 提交于 2019-12-11 04:45:20
问题 I have the following type of query that I wish to convert into a jpa criteria query and the following table structure: Table A 1-->1 Table B 1<--* Table C (proceedings) *-->1 Table D(prcoeedingsstatus) -------- -------- ------- ------- aID bID cID dID ... .... timestamp textValue f_bID .... f_bID f_dID 1 A has 1 B, 1 B has many proceedings and each proceeding has a proceedingstatus. SELECT a.* FROM ((a LEFT JOIN b ON a.f_b = b.id) LEFT JOIN proceedings ON b.id = proceedings.f_b) RIGHT JOIN

How to pass an argument to a function when using ParameterExpression together with org.springframework.data.jpa.domain.Specification?

若如初见. 提交于 2019-12-11 04:08:36
问题 I am using org.springframework.data.jpa.domain.Specification together with JpaSpecificationExecutor to easily create queries with conditions in Java but now I need to call a MySQL DB function that returns an integer value. The problem is that it is unclear how to pass an argument for this function as I am not using TypedQuery: // cb here is CriteriaBuilder ParameterExpression param = cb.parameter(String.class); Predicate predicate = cb.greaterThan(cb.function("A_FUNCTION", Integer.class,

Compare difference between two dates using JPA Criteria Query

泄露秘密 提交于 2019-12-11 03:22:23
问题 I am using MySQL as a backend for my application. I want to execute query below using JPA Criteria Query. SELECT * FROM table1 t1 WHERE datediff(now(), t1.created) >= 20; Note: Type of created is TIMESTAMP I want Criteria Query like: CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); // select CriteriaQuery<Table1> query = cb.createQuery(Table1.class); // from Root<Table1> root = query.from(Table1.class); // where List<Predicate> predicates = new ArrayList<Predicate>(); // TODO:

Hibernate how to use order by clause for sort by sum of field

喜你入骨 提交于 2019-12-10 21:38:47
问题 How to use order by clause for sort by sum of field Entity: class MyClass { private int a; private int b; //... } HQL works correctly: "SELECT myclass FROM MyClass myclass ORDER BY myclass.a + myclass.b DESC " How to do the same thing using Criteria API? 回答1: As you said you could use @Formula @Column(name="column_a") private Integer a; @Column(name="column_b") private Integer b; @Formula("(column_a+column_b)") private Integer c; After that you can sort by property c like criteria.addOrder

How to specify multiple conditions on left join using JPA Criteria API?

放肆的年华 提交于 2019-12-10 19:29:40
问题 I'd like to convert the following SQL query: select * from region_tree country left outer join region_tree region on country.REG_CODE_PAR=region.REG_CODE and region.LFT < country.LFT and region.RGT > country.RGT and region.REG_CODE_PAR = 'ALL' and COUNTRY.STATUS_CODE = 'A' and REGION.STATUS_CODE = 'A into JPA Crtieria based query. I created an entity to represent the self join: @Entity @Table(name = "REGION_TREE") public class RegionTree implements Serializable { ... some other attributes

How to filter child entities collections with predicate?

二次信任 提交于 2019-12-10 13:37:10
问题 I have an entity service on which I need to filter a collection of child entity, based on a list of id's. My service have a public method which receive the id of the parent entity and a list of id's of some of his children entities. By default, I know that JPA will fetch all related entities and this his the actual behavior. But we need to work on the performance of the service. So instead of getting all related entities and filter them with many loop (filter on id's and also on other

JPA Criteria API: how to retrieve date in “mm/dd/yyyy” format

点点圈 提交于 2019-12-10 11:36:31
问题 I want to retrieve one of my column date in "mm/dd/yyyy" format. This column is currently returning date and time both but i want only date in "mm/dd/yyy" format. Below is my postgresql query that i want to convert to criteria api select DISTINCT c.name as Facility, to_char(begin_exam,'mm/dd/yyyy') as begin_exam from a inner join b on a.rad_exam_id = b.id inner join c on c.id = b.site_id group by c.name,to_char(begin_exam,'mm/dd/yyyy') order by c.name,to_char(begin_exam,'mm/dd/yyyy') I

JPA 2 Criteria API: why is isNull being ignored when in conjunction with equal?

那年仲夏 提交于 2019-12-10 10:55:30
问题 I have the following entity class (ID inherited from PersistentObjectSupport class): @Entity public class AmbulanceDeactivation extends PersistentObjectSupport implements Serializable { private static final long serialVersionUID = 1L; @Temporal(TemporalType.DATE) @NotNull private Date beginDate; @Temporal(TemporalType.DATE) private Date endDate; @Size(max = 250) private String reason; @ManyToOne @NotNull private Ambulance ambulance; /* Get/set methods, etc. */ } If I do the following query

JPA CriteriaQuery Order By Latest Child Property

余生颓废 提交于 2019-12-10 10:49:19
问题 I've got 2 entities: Project and ProjectStatus. Project Entity: @Entity public class Project { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id; @OneToMany(mappedBy = "project") private List<ProjectStatus> projectStatusses; } Project Status Entity: @Entity public class ProjectStatus { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Long id; @ManyToOne private Project project; @Enumerated(EnumType.STRING) private ProjectStatusType statusType; } I would like to order

CriteriaBuilder: join one-to-many with ON clause

痴心易碎 提交于 2019-12-10 10:26:08
问题 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