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
0
votes
1 answer

How to select specific entity from joined tables with Hibernate criteria?

I have this SQL statement : select c.* from tableA a inner join tableB b on a.id = b.a_id inner join tableC c on b.id = c.b_id and my criteria is : Criteria criteria = session.createCriteria(tableA.class, "a") …
Mie
  • 23
  • 6
0
votes
2 answers

Hibernate annotations

I created a table in MySQL: 'object_label' with columns 'id' and 'name'. I inserted values to this table. In java I created new class -'ObjectLabel': import javax.persistence.*; @Entity @Table(name = "object_label") public class…
Rivki
  • 107
  • 1
  • 10
0
votes
2 answers

Criteria API and unique results

The next code results with all rows where ispassed=true: Criteria crit = hSession.createCriteria(ResultTable.class); crit.add( Restrictions.eq("ispassed", true)); crit.setProjection(Projections.rowCount()); total = (long) crit.uniqueResult(); But…
rozerro
  • 5,787
  • 9
  • 46
  • 94
0
votes
1 answer

how to query embedded object in Hibernate?

I have classes A, B class A{ @Embedded private B objB; } @Embeddable class B{ Integer x; Integer y; float z; } Now I have a bunch of class A objs Set, I want to query the database, so that rows contain same x and y in Class B (z is not important…
user2146141
  • 155
  • 1
  • 14
0
votes
2 answers

java.lang.ClassCastException: org.hibernate.impl.CriteriaImpl cannot be cast

I am new to Hibernate Criteria , I found many examples using list. Likes to know how a criteria object can be converted to Employee object. Is it possible for Hibernate Criteria to return a class entity. Employee emp= (Employee)…
Cork Kochi
  • 1,783
  • 6
  • 29
  • 45
0
votes
1 answer

Use join and subquery with criteria in hibernate

I searched lot. But can't find solution for my case. i want create hibernate criteria for following query. SELECT * FROM patient as p1 LEFT OUTER JOIN (SELECT * FROM patient_caller_admin_map WHERE caller_admin_id='1') as pca ON…
Thangadurai
  • 524
  • 1
  • 9
  • 20
0
votes
1 answer

Hibernate Criteria Order not working

Am using hibernate criteria to fetch data from database. Now i need to fetch data based on title and createdDate, for that i added if(condition){ criteria.addOrder(Order.asc("title")); }else{ criteria.addOrder(Order.asc("createdDate")); } The…
Anoop LL
  • 1,548
  • 2
  • 21
  • 32
0
votes
1 answer

Hibernate : Do not Fetch Association in criteria APi

I am trying to fetch an entity using criteria API. Here is what header entity looks like public class Header{ @OneToMany(mappedBy = "header", cascade = CascadeType.ALL) @LazyCollection(LazyCollectionOption.FALSE) @JsonManagedReference …
akash
  • 1,801
  • 7
  • 24
  • 42
0
votes
1 answer

Criteria query with projections not fetching one to many collection

So I have 2 hibernate pojos as below class Owner{ Integer id; String name; Integer age; String address; /* Many more fields here */ Set cats; } class Cat{ Owner owner; //referenced from Owner.id String color; } I am querying Owner…
0
votes
1 answer

Changing package name of hibernate managed entities

After doing some refactoring moving some classes into different packages, I started seeing following error while querying the database with criteria builder: java.lang.IllegalArgumentException: Parameter value…
akshay202
  • 586
  • 1
  • 5
  • 23
0
votes
1 answer

Hibernate to get distinct values of one column and one random row value from another column

clm1 clm2 xyz 1 xyz 2 xyz 3 abc 1 abc 2 qwe 5 Suppose i have a table like above. Using something like: criteria =…
2.6.32
  • 25
  • 1
  • 5
0
votes
2 answers

How can I put an if statement inside of a createCriteria in grails?

I'm new in grails and I have a problem. I have a method that receive some data and match the data with a createCriteria and return the data. It's working fine, but that I want to do now if match with the five params that I have in the method and if…
S.P.
  • 2,274
  • 4
  • 26
  • 57
0
votes
1 answer

Getting specific columns from relationships with group by using criteria query

I'm having issues formulating a criteria query. I'm looking at getting distinct Season(id, name), League(id, name) for a specific user, so that I can turn it into a indented…
TechFanDan
  • 3,329
  • 6
  • 46
  • 89
0
votes
0 answers

How to do self join in hibernate query

I have following SQL query. How to map this to hibernate query ? '(' seems causing issue. Is this possible with Criteria Query ? select id from (select prev.id from my_table prev where date = :prevDate) P LEFT OUTER JOIN (select…
Jigar Shah
  • 2,576
  • 6
  • 31
  • 53
0
votes
2 answers

Criteria API in Hibernate, multiple restrictions for same column in joining table

I have tables person and personDoc as below: person : personNo dateofBirth firstname LastName MiddleName salutationCode personDoc : personNo doctype documentNo I have to search person with name = "John" and (doctype = "passport" and documentNo…
Sudersan
  • 25
  • 10