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
14
votes
5 answers

SQL 'LIKE' operator in Hibernate Criteria API

I want to implement some universal filter with Hibernate Criteria. It should work like LIKE operator from SQL: SELECT * FROM table WHERE table.ANYCOLOUMNHERE LIKE '%'||anyvaluehere||'%' I have Map where key is a column name, and…
Divers
  • 9,531
  • 7
  • 45
  • 88
13
votes
2 answers

How to use Hibernate Criteria objects for multiple and/or conditions

I need to create a Hibernate criteria restriction that ors 3 Conditions. The problem is that the last condition is acutally to conditions using the AND operator. My first condition: Criterion startInRange =…
Fred Altman
  • 221
  • 1
  • 2
  • 5
13
votes
1 answer

hibernate.jpa.criteria.BasicPathUsageException: Cannot join to attribute of basic type

I have two tables: Tax and TaxRule. There is one column same in both table i.e TAX_RULE_ID. But don't have any Hibernate mapping like OneToOne or OneToMany. Both table looks like- TAX @Id @Column(name = "TAX_RATE_ID") private Long…
Avinash Mishra
  • 1,346
  • 3
  • 21
  • 41
13
votes
2 answers

Java - Hibernate criteria.setResultTransformer() initializes model fields with default values

I am new to Hibernate and I am trying to get some data from the database. I don't want to get the full data but a projection of an entity. The thing is that in the for-loop when I get the id and the name of my projection, it gets the default values…
Denis Rizov
  • 383
  • 1
  • 4
  • 17
13
votes
1 answer

Hibernate Join using criteria and restrictions

I have 2 entities as PayoutHeader.java @Entity public class PayoutHeader extends GenericDomain implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column private Integer…
Rahul Agrawal
  • 8,913
  • 18
  • 47
  • 59
12
votes
2 answers

How to test Hibernate criteria queries without using any database?

I'm developing a Java application with lots of complex Hibernate criteria queries. I would like to test these criteria to make sure they are selecting the right, and only the right, objects. One approach to this, of course, is to set up an in-memory…
Otavio Macedo
  • 1,542
  • 1
  • 13
  • 34
12
votes
1 answer

Referencing outer criteria query aliases from within an SQLProjection

I am aware that you can use {alias} to refer to the root entity within an SQLProjection: Projections.sqlProjection("MIN({alias}.field) as value", new String[]{"value"}, new Type[]{new LongType()})) What I am trying to do is to reference an alias…
EkcenierK
  • 1,429
  • 1
  • 19
  • 34
12
votes
2 answers

IllegalStateException using CriteriaBuilder for getting a List in Java

Summarizing, I Have 3 entities, the main one is the entity named "Rac". It contains a List of "RacNatureza", which contain a attribute "Natureza". Rac @Entity @Table(name = "rac") public class Rac { @Id @GeneratedValue(strategy…
Murilo Góes de Almeida
  • 1,588
  • 5
  • 20
  • 42
12
votes
1 answer

Hibernate criteria on collection values

I'm trying to put together a complicated query using Hibernate. I've been leaning toward Criteria, but I'm beginning to suspect it's not possible, and so any suggestions would be helpful. I have an entity structure like the following: public class…
Jon
  • 1,249
  • 1
  • 10
  • 21
12
votes
3 answers

Sub-Select In hibernate criteria

I have a sql table A with column names name, id1, id2, val1 and a table B with column names id1, id2, key1, key2 and this is my sql query SELECT v1.id1, v1.id2 FROM ( SELECT A.id1, A.id2, min(val1) AS x …
sumit
  • 3,210
  • 1
  • 19
  • 37
12
votes
4 answers

How to retrieve a complex class and its members using Hibernate Projection?

I have a class as following that need to retrieve from DB using Hibernate. The problem is my class has multiple members and majority of them are classes, how can I retrieve them? @Entity public class Student { @Id long id; String name; …
Jack
  • 6,430
  • 27
  • 80
  • 151
12
votes
4 answers

select "all columns" with "group by" in hibernate criteria queries

I want to write a criteria query using "group by" and want to return all the columns. Plane sql is like this: select * from Tab group by client_name order by creation_time; I understand that it will have count(distinct client_name) number of…
instanceOfObject
  • 2,936
  • 5
  • 49
  • 85
11
votes
2 answers

Spring JPA ExampleMatcher compare date condition

I'm using Spring JPA and get list of data by using Example Matcher. Source code below: public Page findAllByConditions(TranxReportFormModel formModel, Pageable page) { ExampleMatcher matcher = ExampleMatcher.matching() …
11
votes
4 answers

Use Hibernate Criteria for filtering keys and values in Map

I have the following persisted class: public class Code { @ElementCollection(targetClass = CodeValue.class) @MapKeyClass(CodeProperty.class) @JoinTable(name="code_properties") @CreateIfNull( value = false ) private…
micdcar
  • 181
  • 1
  • 11
11
votes
1 answer

How to specify pessimistic lock with Criteria API?

I am retrieving a list of objects in hibernate using Criteria API. However I need lock on those objects as another thread executing at the same time will get the exact objects and only one of the thread will succeed in absence of a pessimistic…
Reddy
  • 8,737
  • 11
  • 55
  • 73