The main advantage of using Criterion API is to build the queries dynamically at run time. But whatever examples I have seen for Criterion API on different sites, it involves single tables. Like
session.createCeiteria("Employee");
Then we keep on adding restrictions on different columns in the employee table.
Is there any way we can involve multiple tables using Criterion API like when we want employees whose department is finance?
So it involves multiple tables, that is, department and employee. I know we can use HQL here, but how do I use criteria here?
Yes, check out the
createAliasmethod of theCriteriaclass.But, you must keep in mind that, when using the Criteria API, you should not think in terms of Tables, instead, you should think in terms of Entities.
That is, the query that you build, is built against the object model, not against the database-model. So, you can only use the
createAliasmethod, when the class (in your example an Employee) you’re building a query for, has an association to the other class.