A type-safe, portable API for defining queries for entities - similar to JPQL
Questions tagged [jpa-criteria]
117 questions
20
votes
1 answer
Spring JPA Specification with Sort
I am using Spring JPA.
To be more precise I am using a Repository which extends JpaRepository and JpaSpecificationExecutor because I require pagination, filtering and sorting.
Now I have the pagination and filtering all working just fine, but I…

NickJ
- 9,380
- 9
- 51
- 74
18
votes
5 answers
JPA CriteriaQuery implements Spring Data Pageable.getSort()
I used JPA CriteriaQuery to build my dynamic Query and pass in a Spring Data Pageable object:
sort=name,desc
At the backend I have a method in my Repository to support dynamic query:
public Page findByCriteria(String username, Pageable page)…

Javatar
- 623
- 3
- 8
- 20
16
votes
4 answers
JPA Specifications by Example
Spring Boot here. I'm trying to wrap my head around JpaRepositories and Specifications when used in the context of implementing complex queries and am struggling to see the "forest through the trees" on several items.
A canonical example of a…

smeeb
- 27,777
- 57
- 250
- 447
13
votes
1 answer
Build JPA Specification from tree
I have created an API which allows users to build out queries using a tree. The tree is built from the SearchOperationRequest class.
@Data
@ApiModel(value = "SearchOperationRequest", description = "Condition for the query")
public class…

Sixthpoint
- 1,161
- 3
- 11
- 34
7
votes
2 answers
Spring data jpa Specification: How to filter a parent object by its children object property
My entity classes are following
@Entity
@table
public class User {
@OneToOne
private UserProfile userProfile;
// others
}
@Entity
@Table
public class UserProfile {
@OneToOne
private Country country;
}
@Entity
@Table
public…

maruf571
- 1,836
- 2
- 21
- 18
7
votes
1 answer
JPA Criteria query eager fetch associated entities using a SINGLE query with joins instead of multiple queries
We are moving from Hibernate native criteria to JPA criteria queries in scope of upgrading a hibernate from 4.3.11 to 5.2.12 and found out different behavior. Previously hibernate criteria use a single query with joins to eager fetch one-to-many…

Yuriy Barannikov
- 149
- 2
- 6
6
votes
3 answers
JPA - How to query using Specification and @EmbeddedId?
I'm using a JPA query that uses a specification to retrieve entities. When I execute the query, I'm getting the error:
org.springframework.data.mapping.PropertyReferenceException: No property name found for type Task!
I've looked at the answers to…

Javaddict
- 251
- 2
- 4
- 12
5
votes
1 answer
spring data jpa specification join fetch is not working
I am trying to use Spring Data JPA Specificaiton to query data, but I got some problem here.
The Java code is as below:
List studentNoticeEntityList = noticeRepository
.findAll((root, criteriaQuery, criteriaBuilder) ->…

Ryan Wang
- 51
- 1
- 3
5
votes
1 answer
Criteriabuilder equal on two columns
When writing queries using Criteriabuilder, how to add an equal condition on two columns? e.g.:
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(String.class);
Root root =…

user1589188
- 5,316
- 17
- 67
- 130
4
votes
2 answers
Criteria API and JPQL API with GROUP BY and GROUP_CONCAT with DISTINCT / ORDER BY / SEPERATOR Support?
Using JPA Criteria API, I want to group by a column and join the values of another column.
For example, the below is the sql approach and I am looking for the equivalent criteria query (and jpql query) approach.
mysql> select *from…

samshers
- 1
- 6
- 37
- 84
4
votes
0 answers
How to build nested object using criteriaBuilder.construct in JPA Criteria Query
I want to query the list of the phone with the person as a phone DTO object but while I construct a DTO object it provides an error.
Phone Entity:
public class Phone {
@Id
@GeneratedValue
private Long id;
private String number;
…

Md. Shamim
- 41
- 1
- 3
4
votes
2 answers
LocalDate between using JPA 2.1 Criteria API
In my entity I have two fields :
private LocalDate startDate = LocalDate.of(1900, 1, 1);
private LocalDate endDate = LocalDate.of(3000, 1, 1);
Using JPA Criteria API I want to select entities where LocalDate.now() > startDate and LocalDate.now() <…

Renaud is Not Bill Gates
- 1,684
- 34
- 105
- 191
3
votes
0 answers
Using Common Table Expression (CTE) in JPA Criteria API
WITH cte AS
(
select
A.A_ID,
B.Lib,
A.Lib,
C.Lib,
(SELECT count(*) FROM X WHERE A.A_ID = X.A_ID) AS countX,
(SELECT count(*) FROM Y WHERE A.A_ID = Y.A_ID) AS countY,
(SELECT count(*) FROM Z…

Renaud is Not Bill Gates
- 1,684
- 34
- 105
- 191
3
votes
2 answers
Converting JPA specification to JPQL
we have JPA Specifications defined on an entity. I want to convert that specification to JPQL So I can create join it with some other entity. (I tried using parent-child relationship but that's not efficient, and that doesn't allow us to put…

yagyavrat
- 107
- 2
- 11
3
votes
2 answers
How to search through array in Spring Boot CrudRepository
Say, I have the following entity class:
Person.java
@Entity
public class Person {
@Id
private String name;
private String[] cars;
// Constructor, getters and setters
}
And the repository:
PersonRepository.java
public interface…

user7346048
- 798
- 1
- 9
- 15