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
11
votes
3 answers

how to write Hibernate Criteria to take nested objects by Projection List?

I want to take Nested object values in Hibernate Projection List. I having Pojo 'Charge' and 'Tariff' class with OneToMany and ManyToOne relations. My sample code is as following: Charge private String id; private Tariff…
Aravinthan K
  • 1,763
  • 2
  • 19
  • 22
11
votes
3 answers

hibernate update single column using criteria

I have a table that contains mamy columns and I want to update the one or few columns of the row without effecting remaining columns I can write query: update table as t set t.a=:a set t.b=:b where t.id=1 But seen I dont know which columns will be…
11
votes
1 answer

Hibernate Criteria for entity with embedded objects

I have an entity "UserDetails" which has the following variables: String userId String userName UserContact userContact (where UserContact is an Embeddable class) UserContact has the following variables: String phoneNumber String email String…
Biman Tripathy
  • 2,766
  • 3
  • 23
  • 27
10
votes
2 answers

Hibernate Criteria Query Issue with Projection and restriction

I am trying to get selected columns from a table using hibernate criteria query Criteria cr = session.createCriteria(OfferCashbackMaster.class) .setProjection(Projections.projectionList() .add(Projections.property("txnType"), "txnType") …
Abhishek Patil
  • 1,373
  • 3
  • 30
  • 62
10
votes
3 answers

How to add a list of Predicates to CriteriaBuilder.or

I have a List to append in an or condition The Issue I am facing is when I am iterating over the List and adding it to the CategoryBuilder then it takes the last Predicate Following is the example: public static Specification
Kalyan Pradhan
  • 1,415
  • 3
  • 19
  • 34
10
votes
2 answers

Hibernate criteria join table issue

I have 3 entities as you can see below. I want to write a query that fetches products. In this query the parameter is a list of optionValues id. now my question is how to join these entities? Product: public class Product{ //other col …
Emil
  • 423
  • 1
  • 12
  • 34
10
votes
4 answers

Null list returned from hibernate query with embedded id

I have an entity with an embedded key. The entity has only the key as a field and the key has 7 fields, some of which can be null. When I run the following query: Criteria criteria =…
mransley
  • 631
  • 7
  • 16
10
votes
3 answers

groovy / grails / unit testing / createCriteria.get

I can mock calls to: MyDomainClass.createCriteria().list{ eq('id',id) eq('anotherParameter',anotherParameterId) } with: def myCriteria = [ list : {Closure cls -> returnThisObject} ] MyDomainClass.metaClass.static.createCriteria = {…
Alex
  • 281
  • 2
  • 8
9
votes
1 answer

Criteria subquery with not null

I want to convert the following subquery to use hibernate subquery: getCurrentSession().createQuery("from Employee where id in (select adminId from Department where adminId is not null)") …
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
9
votes
3 answers

Hibernate criteria query for Collection Table?

I have following Entity @Entity @Table(name = "rule") public class Rule implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "rule_id") private Long id; @ElementCollection(targetClass =…
Anupam Gupta
  • 1,591
  • 8
  • 36
  • 60
9
votes
2 answers

org.hibernate.sql.ast.SqlTreeCreationException: Could not locate TableGroup - model.dao.User(1055362627602899)

I have a spring boot 2.7.5 with hibernate 5.6.12 project and I wanted to upgrade to spring boot 3.1.0 with hibernate 6.6.2 I'm trying a query count in database for getting total number of records in database an I get the…
Adrian
  • 101
  • 8
9
votes
1 answer

How to get batching using the old hibernate criteria?

I'm still using the old org.hibernate.Criteria and get more and more confused about fetch modes. In various queries, I need all of the following variants, so I can't control it via annotations. I'm just switching everything to…
maaartinus
  • 44,714
  • 32
  • 161
  • 320
9
votes
4 answers

Hibernate Criteria and Count Column

I am trying to return an entity with a column that has the count of another table that is a one to many relation. I want to do this using hibernate criteria, not HQL. select p.*, (select count(*) from child where child.parentid = p.id) as…
Mike Flynn
  • 22,342
  • 54
  • 182
  • 341
9
votes
4 answers

How to use key of MAP in Criteria Query?

I have a Bean like this Class TestA { Map testBMap; } Class TestB { String data; ... } I want to fetch the TestA data along with the map testBMap where key ='test1'. How can i do this using Hibernate.
kandarp
  • 4,979
  • 11
  • 34
  • 43
9
votes
2 answers

How to join Multiple tables using hibernate criteria where entity relationship is not direct?

I have three entities. those are: @Entity public class Organization { @Id private long id; @Column private String name; } @Entity public class Book { @Id private Long id; @Column private String name; @ManyToOne …
seal
  • 1,122
  • 5
  • 19
  • 37