Criteria Queries are an objected-oriented approach to building database searches. Where exactly do they fit in though? If you are just using normal SQL in Coldfusion and are comfortable with continuing to do that, criteria is probably not the technology to jump too. If you are have moved to using the built in ORM in Coldfusion and have moved away from writing traditional SQL in preference to HQL you may find have an interest in leveraging Criteria.

In normal SQL in Coldfusion you build up your query inside your query tag, and use if statements and query param tags to dynamically build up the query. This works out decently well. If you make the jump to ORM (built with Java's Hibernate class) you will notice you can do many simple queries using the great built in methods like loadEntity, and ormExecuteQuery. To get some more powerful queries you will need to jump into writing some real HQL. HQL is its own topic, but overall its great. There's a caveat in Coldfusion though. You can't work with it inside the query tags. What ends up happening is this:

view plain print about
1var search = "from apple where id = :applied
2If(searchbydate == true){
3    Search = search + " and date >
:appledate"
4}
5Query = ormGetSession().createQuery(search);
6Query.setParamter('appleid',javacast('int',applied));
7If(searchbydate == true{
8    Query.setDate('appledate',appledate);
9}
10Return Query.list()

You will have to specify every conditional twice. I don't even have to begin to explain how bad, and annoying this is. Now lets build the same search using criteria.

view plain print about
1Criteria = ormGetSession().createCriteria('apple');
2Criteria.add(restriction.eq('appleid',javacast('int',applied)));
3If(searchbyDate = true){
4    Criteria.add(restriction.gt('appledate',parsedatetime(appledate)));
5}
6Return criteria.list();

Much more succinct. The teaser here is you could instead of returning it to a list, is pass the criteria object to another method that further modifies it. Essentially it allows you to leverage a lot of the power of object-oriented programming in your database searches, in a mostly straight forward way.