views:

1006

answers:

3

With HQL I can use dynamic instantiation like this:

select new ItemRow(item.Id, item.Description, bid.Amount)
from Item item join item.Bids bid
where bid.Amount > 100

Now I need to create my queries dynamically with the Criteria API. How can I obtain the same results that I would have obtained with HQL, but using the Criteria API?

Thank you.

+1  A: 

Assuming you are using NHibernate 2.0.1 GA, here is the pertinent documentation:

http://nhforge.org/doc/nh/en/index.html#querycriteria-projection

Hope that helps!

defeated
I'm using 1.2. I'm gonna try and see if that works. Thanks.
sker
+1  A: 

When you use a projection, the return type becomes Object or Object[] instead of the criteria type. You have to use a transformer.

Here is a simple ResultTransformer:

 private class ProjectionTransformer implements ResultTransformer {
        private String[] propertysList;
        private Class<?> classObj;

        /**
         * @param propertysList
         */
        public ProjectionTransformer(String[] propertysList) {
            this.classObj = persistentClass;
            this.propertysList = propertysList;
        }

        /**
         * @param classObj
         * @param propertysList
         */
        public ProjectionTransformer(Class<?> classObj, String[] propertysList) {
            this.classObj = classObj;
            this.propertysList = propertysList;
        }

        @SuppressWarnings("unchecked")
        public List transformList(List arg0) {
            return arg0;
        }

        public Object transformTuple(Object[] resultValues, String[] arg1) {
            Object retVal = null;
            try {
                retVal = Class.forName(classObj.getName()).newInstance();
                int dot = -1;
                for (int i = 0; i < resultValues.length; i++) {
                    if ((dot = propertysList[i].indexOf(".")) > 0) {
                        propertysList[i] = propertysList[i].substring(0, dot);
                    }
                    PropertyUtils.setProperty(retVal, propertysList[i], resultValues[i]);
                }
            } catch (Exception e) {// convert message into a runtimeException, don't need to catch
                throw new RuntimeException(e);
            }
            return retVal;
        }
    }

Here's how you use it:

ProjectionList pl = (...)
String[] projection = new String[]{"Id","Description","Bid.Amount"};
crit.setProjection(pl).setResultTransformer(new ProjectionTransformer(projection));

I have not tested it for relations (eg: Bid.Amount).

Miguel Ping
+4  A: 

You can use the AliasToBean result transformer. (Doc 1.2) It assigns every projection to a property of the same name.

session.CreateCriteria(typeof(Item), "item")
  .CreateCriteria("Bids", "bid")
  .SetProjection(Projections.ProjectionList()
    .Add(Projections.Property("item.Id"), "Id" )
    .Add(Projections.Property("item.Description"), "Description" )
    .Add(Projections.Property("bid.Amount"), "Amount" ))
  .Add(Expression.Gt("bid.Amount", 100))
  .SetResultTransformer(Transformers.AliasToBean(typeof(ItemRow)))
  .List();
Stefan Steinegger
Thanks for this simple gem!
Nicholas Piasecki