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
24
votes
2 answers

setResultTransformer in Criteria

What is the use of setResultTransformer method in criteria API? Can someone explain this with a simple example? I read the javadocs but i am not able to understand them clearly. Regards,
user182944
  • 7,897
  • 33
  • 108
  • 174
23
votes
2 answers

Mapping Java boolean to Oracle Number column with JPA and Hibernate

I have a property created like this in my model: public class Client { private Boolean active; } My RDBMS is Oracle and the active column is of type NUMBER(1,0). How can I use the Restrictions API to achieve the following…
Rafael Roque
  • 377
  • 2
  • 5
  • 14
19
votes
2 answers

Hibernate criteria: how to order by two columns concatenated?

I have a Person table which has two columns: first_name and last_name. The Person class has two corresponding fields: firstName and lastName. Now I'm using criteria api and trying to create an order by based on these two columns concatenated. Is it…
Georgie Porgie
  • 2,100
  • 3
  • 24
  • 25
19
votes
2 answers

How to retrieve a set of member objects using Hibernate?

This question is to follow up with my previous question. I need to retrieve a list of complex classes. Each has a few sets in it and just a specific number of them should be retrieved. I've already read answers of these questions 1,2 but none of…
Jack
  • 6,430
  • 27
  • 80
  • 151
18
votes
1 answer

Using Projections in JPA 2

I need to convert a Hibernate criteria query like the following curList = session.createCriteria(Islem.class) .createAlias("workingDay", "d") .setProjection(Projections.sum("amount")) …
Skyhan
  • 871
  • 3
  • 15
  • 26
18
votes
2 answers

how to do a JOIN FETCH in jpa criteria

I am trying to translate the query below to criteria api. SELECT er from ereturn er JOIN FETCH product_item pi ON pi.ereturn_id = er.id WHERE pi.status = "RECEIVED" To something like this: CriteriaBuilder builder =…
masber
  • 2,875
  • 7
  • 29
  • 49
17
votes
3 answers

Hibernate Restrictions.in vs. Disjunction

Other than less code, what is the difference between the following two approaches to building an IN clause using the Hibernate Criteria API? Are there performance concerns? Is there some logic in the retrieval I am missing? They both seem to…
sma
  • 9,449
  • 8
  • 51
  • 80
16
votes
2 answers

How to handle pagination in JPA (Criteria and Predicates)

I'm fetching the results from DB using criteria and predicates and I got my result list , and I'm trying to apply pagination and sorting but it's not working. Please help me where I'm missing, Here is my code: private Page
Developer
  • 2,389
  • 4
  • 11
  • 17
16
votes
4 answers

getting first row of table by criteria query

How can I get the first row of a table by using criteria or HQL query? Table creation script CREATE TABLE MonthlySubscriber(MSISDN bigint(20) NOT NULL, MonthOfYear int(11) NOT NULL, PRIMARY KEY (MSISDN));
Nikhil Mishra
  • 397
  • 1
  • 6
  • 21
15
votes
5 answers

Criteria API returns a too small resultset

How is this possible, I have to following criteria Criteria criteria = getSession().createCriteria(c); criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); criteria.add(Restrictions.eq("active",true)); List list = criteria.list(); The…
KCL
  • 6,733
  • 10
  • 37
  • 43
15
votes
3 answers

Pagination with Hibernate Criteria and DISTINCT_ROOT_ENTITY

I've have already implemented pagination using the following code: public Paginacao consultarPaginado(int pagina, Integer cidadeId) { Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Anuncio.class); …
jguilhermemv
  • 769
  • 3
  • 11
  • 28
14
votes
0 answers

How can I control the SQL table aliases that Hibernate uses in its generated queries?

tl;dr: Hibernate automatically generates SQL table aliases in its queries like jurisdicti4_ or this_. Here's an example query: SELECT this_.id AS id2_6_3_, this_.a_table_column AS…
Kaypro II
  • 3,210
  • 8
  • 30
  • 41
14
votes
1 answer

Could not get a field value by reflection getter

I'm trying to filter a result set by a foreign key: createCriteria(Person.class).add(Restrictions.ne("position", 1L)).list() But getting this exception: org.hibernate.PropertyAccessException: could not get a field value by reflection getter of…
dtrunk
  • 4,685
  • 17
  • 65
  • 109
14
votes
1 answer

Does Hibernate Criteria Api completely protect from SQL Injection

I am working with Hibernate to protect my website from SQL Injection. I heard that Hibernate Criteria API is more powerful than HQL. Does Hibernate Criteria Api completely protect from SQL Injection?
ѕтƒ
  • 3,547
  • 10
  • 47
  • 78
14
votes
4 answers

Criteria eager fetch-joined collection to avoid n+1 selects

Let's say Item and Bid are entities: an Item has many Bids. They are mapped in Hibernate in a typical parent/child relationship: ...
Camilo Silva
  • 8,283
  • 4
  • 41
  • 61