Questions tagged [hibernate-criteria]

The Criteria interface of Hibernate ORM , represents a query against a particular persistent class. The interface provides access to the powerful mechanism of hibernate criteria API, that allows the programmatic creation of queries against the DB.

Hibernate provides an alternate way of HQL to manipulate objects and it is called Hibernate Criteria Query. The Hibernate Session interface provides createCriteria() method which can be used to create a Criteria object. This criteria object returns instances of the persistence object's class when the application executes a criteria query.

The primary advantage of Criteria Query over HQL and native SQL is that it allows OOP control over the queries and hence it is more dynamic compared to HQL. In order to make HQL dynamic String concatenation needs to be done, which is not considered as a good programming concept.

To learn more about Criteria Query and it's usage the official website of Hibernate is a very good resource.

1659 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
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
72
votes
5 answers

Hibernate Criteria vs HQL: which is faster?

I have been reading some anwers, but i'm still confused. ¿Why? because the differences that you have mentioned do not relate with the performance. they are related with easy use.(Objetc(criteria) and SQL(hql)). But I would like to know if "criteria"…
kcobuntu
  • 751
  • 1
  • 6
  • 4
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
57
votes
5 answers

Hibernate Criteria Query to get specific columns

I am using Criteria Query in my code. It always fires select * from ... Instead I want to neglect one column(field) from my query as that field have large number of data stored in bytes. And that causing performance issue. Can any one give an idea…
Ketan Bhavsar
  • 5,338
  • 9
  • 38
  • 69
50
votes
4 answers

When to use Hibernate projections?

I am a little confused about Hibernate's projections and criteria. When to use projections and when to use criteria?
reddy
  • 701
  • 2
  • 10
  • 10
46
votes
2 answers

Criteria.DISTINCT_ROOT_ENTITY vs Projections.distinct

I am pretty new to Hibernate. I found out that we can get distinct result using following two different ways. Could any one tell me what is the difference between them? When to use one over…
user3123690
  • 1,053
  • 5
  • 17
  • 27
42
votes
4 answers

Hibernate Group by Criteria Object

I would like to implement the following SQL query with Hibernate Criteria: SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name value GROUP BY column_name I have tried to implement this with Hibernate…
Bomberlatinos9
  • 790
  • 3
  • 11
  • 24
41
votes
3 answers

org.hibernate.QueryException: could not resolve property: filename

I am using Hibernate Criteria to get values from column filename in my table contaque_recording_log. But when I'm getting the result, it throws an exception org.hibernate.QueryException: could not resolve property: filename of:…
39
votes
1 answer

Why is criteria query deprecated in Hibernate 5?

As we already know, criterion query is deprecated in Hibernate 5. It was such a useful feature in the previous versions of Hibernate. And it still performs better than HQL. So what is the reason of it's deprecation in Hibernate 5 ? And also this…
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
37
votes
3 answers

How do you select a column using Hibernate?

I would like to select a single column instead of a whole object, using Hibernate. So far I have this: List firstname = null; firstname = getSession().createCriteria(People.class).list(); My problem is that the above code returns the…
user1191027
29
votes
6 answers

How to order result of hibernate based on a specific order

I need to send a query to retrieve values that has a specific group of characters as following: Lets say I am interested in 'XX' so it should search for any field that its value starts with 'XX' or has ' XX' (space XX). For example XXCDEF, PD XXRF…
Jack
  • 6,430
  • 27
  • 80
  • 151
27
votes
2 answers

Hibernate Criteria join query one to many

I have a Cat class and a Owner class. A cat has one owner but an owner can have many cats. What I want to query is get all owners who have a cat with blue eyes. class Cat { Owner owner; //referenced from Owner.id String eyeColor; } class…
hellzone
  • 5,393
  • 25
  • 82
  • 148
24
votes
1 answer

Using Hibernate's Criteria and Projections to Select Multiple Distinct Columns

Using Hibernate's Criteria, I want to execute the equivalent of: select distinct uspscity, state from citycomplete where USPSCITY = 'HOUSTON' I thought doing the following would yield the results I wanted: ProjectionList projList = new…
bvulaj
  • 5,023
  • 5
  • 31
  • 45
24
votes
1 answer

Hibernate CriteriaBuilder to join multiple tables

I'm trying to join 4 tables using hibernate criteriabuilder.. Below are the tables respectively.. ` @Entity public class BuildDetails { @Id private long id; @Column private String buildNumber; @Column private String…
ktgirish
  • 257
  • 1
  • 2
  • 12
1
2 3
99 100