Questions tagged [criteria]

Mechanism typical to ORM frameworks that allows the creation of queries against a database in a dynamic and object-oriented fashion. When writing a query using Criteria one uses an API instead of writing a query using a defined language (SQL).

Mechanism typical to ORM frameworks that allows the creation of queries against a database in a dynamic and object-oriented fashion. When writing a query using Criteria one uses an API instead of writing a query using a defined language (SQL).

All the most known ORM frameworks provide Criteria APIs, like Hibernate Criteria and after the release of JPA it was standardized as JPA Criteria.

2829 questions
312
votes
22 answers

JPA and Hibernate - Criteria vs. JPQL or HQL

What are the pros and cons of using Criteria or HQL? The Criteria API is a nice object-oriented way to express queries in Hibernate, but sometimes Criteria Queries are more difficult to understand/build than HQL. When do you use Criteria and when…
cretzel
  • 19,864
  • 19
  • 58
  • 71
79
votes
6 answers

Hibernate criteria: Joining table without a mapped association

I'd like to use Hibernate's criteria api to formulate a particular query that joins two entities. Let's say I have two entities, Pet and Owner with a owner having many pets, but crucially that association is not mapped in the Java annotations or…
Snukker
  • 1,963
  • 3
  • 18
  • 18
79
votes
9 answers

How to get SQL from Hibernate Criteria API (*not* for logging)

Is there a way to get the (to-be-generated) SQL from a Hibernate Criteria? Ideally, I would have something like: Criteria criteria = session.createCriteria(Operator.class); ... build up the criteria ... ... and then do something like ... String…
David Bulté
  • 2,988
  • 3
  • 31
  • 43
76
votes
10 answers

How to get distinct results in hibernate with joins and row-based limiting (paging)?

I'm trying to implement paging using row-based limiting (for example: setFirstResult(5) and setMaxResults(10)) on a Hibernate Criteria query that has joins to other tables. Understandably, data is getting cut off randomly; and the reason for that is…
Daniel Alexiuc
  • 13,078
  • 9
  • 58
  • 73
73
votes
2 answers

Hibernate Criteria Restrictions AND / OR combination

How would I achieve this using Hibernate Restrictions? (((A='X') and (B in('X',Y))) or ((A='Y') and (B='Z')))
girishsovflow
  • 735
  • 1
  • 6
  • 5
67
votes
3 answers

How to properly express JPQL "join fetch" with "where" clause as JPA 2 CriteriaQuery?

Consider the following JPQL query: SELECT foo FROM Foo foo INNER JOIN FETCH foo.bar bar WHERE bar.baz = :baz I'm trying to translate this into a Criteria query. This is as far as I have gotten: CriteriaBuilder cb =…
chris
  • 2,541
  • 1
  • 23
  • 40
65
votes
9 answers

JPA2: Case-insensitive like matching anywhere

I have been using Hibernate Restrictions in JPA 1.0 ( Hibernate driver ). There is defined Restrictions.ilike("column","keyword", MatchMode.ANYWHERE) which tests if the keyword matching the column anywhere and it is case-insensitive. Now, I am…
Gaim
  • 6,734
  • 4
  • 38
  • 58
64
votes
1 answer

Hibernate Criteria Join with 3 Tables

I am looking for a hibernate criteria to get following: Dokument.class is mapped to Role roleId Role.class has a ContactPerson contactId Contact.class FirstName LastName I want to search for First or LastName on the Contact class and retrieve a list…
mahatmanich
  • 10,791
  • 5
  • 63
  • 82
53
votes
3 answers

How to achieve "not in" by using Restrictions and criteria in Hibernate?

I have list of category. I need a list of category by excluding 2,3 row. Can we achieve through hibernate by using Criteria and Restriction?
Shashi
  • 12,487
  • 17
  • 65
  • 111
52
votes
1 answer

How do you order a oneToMany join table in hibernate criteria

Say I have a class Mother with a oneToMany mapping to Kittens @Entity @org.hibernate.annotations.Entity(dynamicUpdate = true) @Table(name = "Mother") ..... @OneToMany(fetch = FetchType.LAZY, targetEntity=Kittens.class,…
jaseFace
  • 1,415
  • 5
  • 22
  • 34
50
votes
8 answers

Getting a count of rows in a datatable that meet certain criteria

I have a datatable, dtFoo, and would like to get a count of the rows that meet a certain criteria. EDIT: This data is not stored in a database, so using SQL is not an option. In the past, I've used the following two methods to accomplish…
Sesame
  • 3,370
  • 18
  • 50
  • 75
46
votes
8 answers

UNION to JPA Query

Is it possible to query "UNION" in JPA and even "Criteria Builder"? I'm looking for examples, but so far i got no result. Does anyone have any examples of how to use it? Or would that be with native sql?
Giovane
  • 753
  • 4
  • 9
  • 15
45
votes
4 answers

Cannot be cast to java.io.Serializable

I am currently using criteria to retrieve the details of a user, but when trying to query the details object with the right user, I get a ClassCastException. My Criteria Code; Criteria criteria =…
Thizzer
  • 16,153
  • 28
  • 98
  • 139
42
votes
4 answers

How do I use a complex criteria inside a doctrine 2 entity's repository?

Lets say I have a table that holds information about festivals. Each festival has a start and end date. I want to select all the festivals that are live (that happen) on a given date. Meaning, I want to select all the festivals that their start date…
Doron
  • 3,176
  • 7
  • 35
  • 60
41
votes
2 answers

Hibernate query a foreign key field with ID

For example, I have two entities: Employee and Address. Of these enitities, Employee has a foreign key AddressID references the ID column on Address. In the Java domain objects, Hibernate nicely wraps the forgein key integer field with a Address…
savanna
  • 739
  • 3
  • 7
  • 11
1
2 3
99 100