Questions tagged [criteriaquery]
245 questions
144
votes
6 answers
In JPA 2, using a CriteriaQuery, how to count results
I am rather new to JPA 2 and it's CriteriaBuilder / CriteriaQuery API:
CriteriaQuery javadoc
CriteriaQuery in the Java EE 6 tutorial
I would like to count the results of a CriteriaQuery without actually retrieving them. Is that possible, I did not…

Sean Patrick Floyd
- 292,901
- 67
- 465
- 588
63
votes
7 answers
JPA Query selecting only specific columns without using Criteria Query?
Is it possible to select, say, only properties A and B from an object using a JPA query without using criteria queries?
To select all properties I'd just do something like:
SELECT i FROM ObjectName i WHERE i.id = 10
But I have an object with many…

Edy Bourne
- 5,679
- 13
- 53
- 101
17
votes
3 answers
JPA Criteria Query - How to Avoiding Duplicate Joins
I need to make a criteria query with a lot of conditional joins and where clauses, in such cases the code tends become complex and could be produces duplicate joins.
For instance i have the following structure of Tables and JPA entities :
ACCOUNT
…

Eduardo Fabricio
- 2,151
- 2
- 25
- 32
17
votes
1 answer
Condition left join in CriteriaQuery
Hi everybody I am trying to do this in CriteriaQuery, I was searching so long but I can't found anything to do it, someone can help me?
SELECT b.name
FROM Empl a
LEFT OUTER JOIN Deplo b ON (a.id_depl = b.id_depl) AND b.id_place = 2;
I'm just…

Ricardo Romero Benítez
- 712
- 2
- 7
- 20
15
votes
1 answer
Polymorphic CriteriaQuery without inverse relationship
I have the following EJB structure. Don't wonder about Animal and Inventory, these classes are only here to demonstrate the structure in a simplified way (Update: I have revised the class names to construct a better understandable example. Another…

Thor
- 6,607
- 13
- 62
- 96
15
votes
2 answers
JPA Criteria query group by uses only the id
This is a sample entity:
public class Account{
@Id
Long id
Double remaining;
@ManyToOne
AccountType type
}
public class AccountType{
@Id
Long id;
String name;
}
Now i create a criteria query with Join as follwing : …

Rasoul Taheri
- 802
- 3
- 16
- 32
13
votes
3 answers
jpa 2 hibernate limit (max results) to a CriteriaQuery
maybe it's a silly question but I cannot find the answer in the docs:
How can set a limit to the CriteriaQuery using JPA2?
Thanks

Hugo
- 2,139
- 4
- 22
- 30
8
votes
1 answer
How to check a collection size in JPA2
Consider the following:
@Entity
public class Book
{
private List authors;
@ElementCollection
public List getAuthors() {
return authors;
}
public void setAuthors(List authors) {
…

Sergey Beryozkin
- 688
- 1
- 4
- 9
7
votes
1 answer
Why doesn't JPA CriteriaQuery provide update query?
CriteriaQuery in JPA2.0 provides a type-safe way to do select, it's great. But I am wondering why it won't provide update/delete operation? To bulk update/delete, you have to fall back to old time writing error-prone SQL or JPQL text. IMO,…

zx_wing
- 1,918
- 3
- 26
- 39
7
votes
5 answers
Is there a way to reduce the amount of boiler-plate code associated with a CriteriaQuery (in JPA 2.0)?
I love the type safety CriteriaQuery brings ing JPA 2.0 but it also brings a bit of boiler-plate code. For example, let say I have an entity called NamedEntity, which simply has an id and a String field called "name" (assume it has the unique…

Andrey
- 8,882
- 10
- 58
- 82
6
votes
2 answers
Selecting generic primary key with CriteriaQuery
When migrating from Hibernate Criteria api to CriteriaQuery I ran into a generic DAO for a abstract class that has a where on a common field but does a select on their id, even if the ids are totally different per class.
The old projection looks…

Jeppz
- 912
- 1
- 8
- 31
6
votes
1 answer
Spring JPA Specification Not Exists and Self Join
How to build the following SQL query in Spring JPA Specification?
SELECT col1, col2, MAX(col3)
FROM table_name t1
WHERE col4 IN (1,2,3) AND status IN ('STATUS_1','STATUS_2') AND
NOT EXISTS
(
SELECT 1 FROM table_name t2 WHERE t1.id =…

Saravanan
- 604
- 1
- 8
- 20
6
votes
0 answers
Criteria Query with BigMoney from Joda Money (Multi-Column field in Entity)
I have a serious issue with Criteria Query.
My entity class looks like this:
class X {
...
@Columns(columns = {
@Column(name = "priceCurrency", nullable = false),
@Column(name = "priceAmount", nullable = false)})
@Type(type =…

Benjamin M
- 23,599
- 32
- 121
- 201
5
votes
1 answer
jpa criteria query duplicate values in fetched list
I'm observing what I think is an unexpected behaviour in JPA 2 when fetching a list attribute with a criteria query.
My query is as follows (an extract of it):
CriteriaBuilder b = em.getCriteriaBuilder();
CriteriaQuery c =…

comandante N
- 276
- 2
- 5
- 11
5
votes
1 answer
JPA 2 CriteriaQuery Question
I am just starting out with JPA 2 criteria query API and finding it tough to learn. Looked around the net a bit, but haven't found good examples/tutorials yet. Can someone suggest a good tutorial and/or help me with the following simple query I am…

Naresh
- 23,937
- 33
- 132
- 204