commit
commit

Reputation: 4807

Difference between createCriteria and createAlias

I was reading hibernate criteria document here:

http://docs.jboss.org/hibernate/orm/3.6/javadocs/org/hibernate/Criteria.html

I had used it many time and I am normally use createAlias() to join table, here they have provide two methods to make a join and fetch data from the tables, there are:

 List cats = session.createCriteria(Cat.class)
     .createCriteria("kittens")
         .add( Restrictions.like("name", "Iz%") )
     .list();

 List cats = session.createCriteria(Cat.class)
     .createAlias("kittens", "kit")
     .add( Restrictions.like("kit.name", "Iz%") )
     .list();

So I am not able to distinguish difference between .createCriteria("kittens") and createAlias("kittens", "kit") or may be I am not getting what this code exactly do, can someone help me to clear my confusion.

Upvotes: 3

Views: 11580

Answers (2)

Ankur Lathi
Ankur Lathi

Reputation: 7836

The only difference is that CreateCriteria has 2 additional overloads without the alias parameter, this difference is long gone in the latest versions.

But essentially the application is slightly different in its usage is that CreateCriteria uses its relations of mapping from parent to child, whilst with CreateAlias you defined them with your customized alias names from the root.

Read more from here.

Upvotes: 4

harrybvp
harrybvp

Reputation: 2505

Main Difference is that Criterias' createCriteria() creates and returns Sub Criteria (new Criteria Object).This is useful if you want to create criteria for subquery.
Here is what documentation says about its return type

Returns:
the created "sub criteria"

Criteria's CreateAlias() returns existing Criteria Object
Here is what documentation says about its return type

Returns:
this (for method chaining)

Upvotes: 2

Related Questions