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

Criteria.DISTINCT_ROOT_ENTITY doesn't prevent duplicated objects

I have following dao method: @Override public List findAll() { Session session = sessionFactory.getCurrentSession(); Criteria criteria = session.createCriteria(AdminRole.class); …
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
9
votes
3 answers

Hibernate Criteria: Left Outer Join with restrictions on both tables

I am doing a LEFT OUTER JOIN, but I am only able to apply Restrictions on the first table. Is there a way ti apply on the second table as well? Here is my code: Criteria criteria = this.crudService …
Markos Fragkakis
  • 7,499
  • 18
  • 65
  • 103
9
votes
1 answer

when or why do i use Property.forName()?

What is the difference between : List cats = session.createCriteria(Cat.class) .add( Restrictions.like("name", "F%") .list(); and List cats = session.createCriteria(Cat.class) .add( Property.forName("name").like("F%") ) .list(); Or for…
insanity
  • 347
  • 3
  • 14
9
votes
1 answer

Query @ElementCollection JPA

I have an Entity Transaction as following : @Entity class Transaction extends AbstractEntity{ private static final long serialVersionUID = 7222139865127600245L; //other attributes @ElementCollection(fetch =…
prayagupa
  • 30,204
  • 14
  • 155
  • 192
9
votes
4 answers

Best way to access nullable values in c#

In our C# code, we already test if a variable is null before attempting to access it. if (myInt.HasValue) { var yourInt = myInt; // do something with yourInt } My question: is there a difference in using the nullable property as if it wasn't…
shanabus
  • 12,989
  • 6
  • 52
  • 78
8
votes
1 answer

Hibernate Criteria Transformers.aliasToBean is not populating correct values

I am trying to create BO by joining my entity classes Criteria criteria = session.createCriteria(Report.class,"r"); criteria .createAlias("template", "t") .createAlias("constituents", "rc") .createAlias("rc.entity", "pe") …
Rohit
  • 353
  • 2
  • 8
  • 24
8
votes
4 answers

Hibernate criteria with restrictions on children

I have a Hibernate criteria call that I want to execute in one SQL statement. What I'm trying to do is select instances of Parent that have Children with a property in a range of values (SQL IN clause), all while loading the children using an outer…
jonathan.cone
  • 6,592
  • 2
  • 30
  • 30
8
votes
4 answers

Convert SQL Query to Hibernate Criteria and Projections

I have query from SQL : select e.* from terminal_remote_deployment e where id = (select top 1 e1.id from terminal_remote_deployment e1 where e1.Terminal_info_id = e.Terminal_info_id order by e1.version desc …
Talib
  • 1,134
  • 5
  • 31
  • 58
8
votes
1 answer

Hibernate criteria api 'Select in'

is it possible to create a 'select in'-query with the hibernate critiria api ? Example : I have two tables in a 1:n relation, company and department select * from company c where c.id in (select company_id from department d where d.departmentname =…
ABX
  • 1,173
  • 2
  • 22
  • 39
8
votes
1 answer

Using Hibernate Criteria API, how to query for a subproperty of a property that exists only for certain property types

Consider classes Account, RealAccount, VirtualAccount, and Operation such that: class Account { } class RealAccount extends Account { String name; } class VirtualAccount extends Account { } class Operation { Account account; } This means…
Marcelo Glasberg
  • 29,013
  • 23
  • 109
  • 133
8
votes
3 answers

How to make HIbernate fetch all properties of root entity and only specific properties of associated entity?

I have root entity Hostel and its single association User owner. When I fetch Hostel entity I need to eagerly fetch User owner, but only owner's 3 properties : userId,firstName,lastName. For now my criteria query is : Criteria criteria =…
Volodymyr Levytskyi
  • 3,364
  • 9
  • 46
  • 83
8
votes
4 answers

Left join using hibernate criteria

I have two entity: Issue and Issue_Tracker. I am using Hibernate 3.6. SELECT `issues`.`issue_id`, `issues`.`issue_raised_date`, `issues`.`issue_description`, `issue_tracker`.`tracker_status` FROM `issues` LEFT JOIN …
arvin_codeHunk
  • 2,328
  • 8
  • 32
  • 47
8
votes
2 answers

Hibernate update query with innerjoin

I have the following MySQL update query with inner join: UPDATE Country AS c INNER JOIN State s ON c.CountryID = s.CountryID INNER JOIN City cy On s.StateID = cy.StateID SET c.Active='Y', s.Active='Y',cy.Active='Y' WHERE …
edaklij
  • 4,121
  • 11
  • 31
  • 43
7
votes
1 answer

Hibernate - Orderding criteria by formula property

Say I have an entity MyEntity, and it has a formula-based property fmlaProp. Now say I create a criteria: s.createCriteria(MyEntity.class) .setProjection( Projections.distinct( Projections.property("fmlaProp"))) …
Andrey Balaguta
  • 1,308
  • 2
  • 21
  • 28
7
votes
2 answers

Query Subset of Columns with Gorm

Suppose I have the following Domain class: class Book { String title String author byte[] largeCoverArtImage } I have a list view where I do not need to display largeCoverArtImage, how can I perform the following SQL query using GORM…
Stephen Swensen
  • 22,107
  • 9
  • 81
  • 136