Assuming the following scenario:
class Project{
public Job Job;
}
class Job{
public Name;
}
Assuming I want to use the Criteria API to search for all projects whose Job has the name "sumthing".
I could use the CreateAlias to create an alias for Job and use it to access Name, or I could create a new Criteria for the property Job and search by Name.
Performance wise, is there any difference?
To explain the difference between CreateCriteria and CreateAlias in NHibernate 2.0 + lets see the following domain model.
Now if you write following criteria to inner join these entities
The above criteria wont work because when the first CreateCriteria runs it return "Category" entity, therefore when the second CreateCriteria execute it wont find property ProductStocks in the "Category" entity and the query will fail.
So the correct way to write this criteria is
When the first CreateAlias runs it return "Product" entity, when the second CreateCriteria execute it will find property ProductStocks in the "Product" entity.
So the TSQL will be like this.
I hope this will help.
given these requirements there would be no difference, the generated SQL is the same: for mappings:
and classes
these criteria definitions
generate the same SQL
note however that the
CreateAlias
relies on the mappings to generate associations whereas theCreateCriteria
call allows to specifyJoinType
.so, these calls
generate these SQL statements
createAlias() returns original criteria as is result createCriteria() returns new criteria constructed with createCriteria
difference will be when chaining methods e.g.
cr.createAlias().add(Restrictions.ilike("code","abc")) will add restriction to entity cr.createCriteria("parent","p").add(Restrictions.ilike("code","abc")) will add restriction to its parent