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

How to pass value to sqlRestriction of hibernate?

I need to retrieve items based on a few different restrictions, one is to have code of 234 the other is to have calculated number of less than 10, but I am not sure how to pass values to the sqlRestrictions method. I am using {alias} but it passes…
Daniel Newtown
  • 2,873
  • 8
  • 30
  • 64
7
votes
1 answer

Hibernate criteria with EmbeddedId

I have my Entity: @Entity @Table(name="performances") @AssociationOverrides({ @AssociationOverride(name="id.player", joinColumns=@JoinColumn(name="player_id")), @AssociationOverride(name="id.season",…
Cichy
  • 1,319
  • 3
  • 20
  • 36
7
votes
1 answer

Defining OR Condition with grails criteria api

I have the following domain objects: class User { String name Transaction transaction static constraints = { transaction nullable: true } } class Transaction { boolean successful User user static…
micha
  • 47,774
  • 16
  • 73
  • 80
6
votes
3 answers

Hibernate 6 SQLFunctionTemplate Alternative

We have HibernateMetadataBuilderContributor Like below. Which works in Hibernate 5 or Spring boot 2.7. But not working when we migrate to Hibernate 6 or Spring boot 3. public class HibernateMetadataBuilderContributor implements…
6
votes
1 answer

Hibernate 5 change not to use fetch first rows only

I'm using Hibernate 5.2 with oracle 11 which does not support fetch first rows only and I need to get back to old style hibernate. is there any hibernate configuration to do that ?
Omid P
  • 211
  • 2
  • 10
6
votes
1 answer

What is the fastest way to update multiple rows of a table

I have a table named messages containing toUser, message and status columns. I want to update all the statuses of the messages with a specific user. So, I wrote a query like this, Session s = DB.getSession(); s.createSQLQuery("UPDATE `message` SET…
Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
6
votes
2 answers

Hibernate, getting duplicate values

I am writing a very simple query, but I am getting duplicate values for some reason. Criteria cr = session.createCriteria(ProcessInstance.class, "p") .add(Restrictions.isNull("end")); @Cleanup ScrollableResults sr =…
Shervin Asgari
  • 23,901
  • 30
  • 103
  • 143
6
votes
1 answer

Hibernate Performance issue

I am very new to Hibernate. Here I would like to compare two option. First option My hibernate pojo classes as follows. Stock { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "stock_id") private Long stockId; …
Alex
  • 790
  • 1
  • 7
  • 22
6
votes
3 answers

How to get over limitations of the Hibernate Criteria and Example APIs?

I'm in a position where our company has a database search service that is highly configurable, for which it's very useful to configure queries in a programmatic fashion. The Criteria API is powerful but when one of our developers refactors one of…
Anthony Bishopric
  • 1,306
  • 11
  • 23
6
votes
1 answer

When using Entity Graphs in JPA 2.1, is there a way of using the metamodel when there are Subgraphs of Subgraphs?

For example, consider a Customer entity has a Set of Orders. Each Order has a Set of OrderItems. I can do this with named attributes: EntityGraph eg = em.createEntityGraph(Customer.class); Subgraph egChild =…
Jim Cox
  • 974
  • 7
  • 10
6
votes
2 answers

Hibernate Projections / Lazy Loading for non required 1 to 1 Mappings

I have the following 2 classes (trimmed down for this post) public class ApplicationVO implements Serializable { /** * */ private static final long serialVersionUID = -3314933694797958587L; @Id @GeneratedValue(strategy =…
6
votes
3 answers

How can you remove a criterion from criteria?

For instance if I do something like: Criteria c = session.createCriteria(Book.class) .add(Expression.ge("release",reDate); .add(Expression.ge("price",price); .addOrder( Order.asc("date") ) …
ChuckM
  • 75
  • 1
  • 4
6
votes
2 answers

using mysql "order by case" in hibernate criteria

I'm using Hibernate 4.3.1 final, Mysql 5.5 and I want to use an "order by case" order logic on some joined entities. A pure sql representation of what I wish to achieve would look something like: select adv.id, adv.published_date from advert as…
Simon B
  • 1,784
  • 3
  • 21
  • 26
6
votes
1 answer

Spring Data Transformers.aliasToBean(AgentRecordDTO.class)

I want to use Hibernate Transformation with Spring Data. I have an entity AgentRecord with attributes as @Entity public class AgentRecord extends AbstractEntity { @ManyToOne private User processedBy; private String…
prayagupa
  • 30,204
  • 14
  • 155
  • 192
6
votes
1 answer

Hibernate criteria query multiple criteria

In my current project I've faced a problem of getting entities with hibernate criteria query. I have the following entities: Professor, which contains a list of students Student, which contains a list of assignments. Assignment, which contains id…
ilya.stmn
  • 1,604
  • 5
  • 23
  • 41