Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do a custom projection with the Criteria API in NHibernate?

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.

like image 226
sker Avatar asked Jan 23 '23 17:01

sker


2 Answers

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();
like image 118
Stefan Steinegger Avatar answered Jan 26 '23 05:01

Stefan Steinegger


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).

like image 28
Miguel Ping Avatar answered Jan 26 '23 06:01

Miguel Ping