Simple queries

Suppose we want to get the employees whose Lastname is equal to "Smith" or "Doe" and salary is greater than 700. In Nhibernate Criteria Builder we specify every our expectation by executing the setters of recorder properties:

//composing business query
EmployeeRec employeeRec = new EmployeeRec();
employeeRec.LastName.SetEq("Smith").Or.SetEq("Doe");
employeeRec.Salary.SetGt(700);
//SetEq() = "is equal", SetGt() = "is greater than"


Let's allow NHibernate to do the dirty job and generate SQL query:

DetachedCriteria criteria = employeeRec.BuildCriteria();
object queryResult = criteria.GetExecutableCriteria(session).List();


And here is the SQL generated by NHibernate:

SELECT THIS_.ID          AS ID2_0_,
       THIS_.FIRSTNAME   AS FIRSTNAME2_0_,
       THIS_.LASTNAME    AS LASTNAME2_0_,
       THIS_.DATEOFBIRTH AS DATEOFBI4_2_0_,
       THIS_.SALARY      AS SALARY2_0_,
       THIS_.COMPANYID   AS COMPANYID2_0_,
       THIS_.JOBID       AS JOBID2_0_
FROM   BSF.EMPLOYEES THIS_
WHERE  (THIS_.LASTNAME = @p0
         OR THIS_.LASTNAME = @p1)
       AND THIS_.SALARY > @p2


As you can see - using recorders is very easy! We've got full intellisense support here, and thanks to recorders we can define our demands to each DTO property (equal, greater than, between, etc.)

Last edited Oct 16, 2008 at 8:12 PM by infomechanica, version 5

Comments

No comments yet.