These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: HQL query with SetResulTransformer not working
PostPosted: Thu May 03, 2007 4:19 am 
Newbie

Joined: Thu May 03, 2007 4:08 am
Posts: 1
I think this might be a bug. I have a HQL query that queries for some scalar values. I want to map this on a simple class that is not mapped in any mapping files. When i add the AliasToBean transformer:

query.SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof(MyDto)));

I get a NullReference exception, when i use the transformer that uses a constructor, it works fine.

I think this has something to do with not being able to add an alias to the the select clause in HQL for NHibernate.

Can someone confirm this, or give me an indication of what i might be doing wrong?

TIA.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 31, 2007 3:31 pm 
Newbie

Joined: Fri Jun 16, 2006 9:19 am
Posts: 7
Having the same problem here. Anyone could shed a light?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 04, 2007 4:07 am 
Newbie

Joined: Wed Feb 28, 2007 11:52 am
Posts: 4
Did you declare the fields to use to map to the bean with sqlQuery.AddScalar ? This was undocumented when I tried this functionnality.

Here is a working example :

string requete = "select " +
"c.NONOMB as NumeroMeuble, " +
"c.CDTTPA as CodificationTitre, " +
"c.NOMBCS as NumeroCase, " +
"c.QTRPCR as QuantiteRepartieCorrigee, " +
"m.NBEXAP as NbreAppoints, " +
"m.NBPQCP as NbrePaquetsComplets, " +
"m.NBPQOV as NbrePaquetsOuverts, " +
"m.NBEXTO as TotalExemplaires, " +
"e.K9I3NH as IndiceSouchage " +
"from TCASPA c left join TMBLPA m ON c.NOPMDP=m.NOPMDP and c.CDRSDP=m.CDRSDP and c.CDTTPA=m.CDTTPA and c.NONOPA=m.NONOPA and c.CDSFPA=m.CDSFPA and c.CDEDPA=m.CDEDPA and c.TYPRPA=m.TYPRPA and c.DTPRPA=m.DTPRPA and c.NONOMB=m.NONOMB " +
"left join YELSPD e ON e.K9BECD=c.NOPMDP and e.K9EGST=c.CDRSDP and e.K9ALCE=c.CDTTPA " +
"where c.DTPRPA=:datePreparation and c.CDRSDP=:codeReseau and c.NOPMDP=:numeroDepot and c.CDTTPA=:codeTitre and c.NONOPA=:numeroParution and c.CDSFPA=:suffixeParution and c.CDEDPA=:codeEdition and c.TYPRPA=:typePreparation " +
"order by NumeroMeuble, NumeroCase";

ISQLQuery sqlQuery = TransactionManager.GetHibernateSession().CreateSQLQuery(requete);
sqlQuery.SetResultTransformer(Transformers.AliasToBean(typeof(ElementDST302)));
sqlQuery.SetFlushMode(FlushMode.Never);

// champs en sortie
sqlQuery.AddScalar("NumeroMeuble", TrimmedChar.Type);
sqlQuery.AddScalar("CodificationTitre", NHibernateUtil.String);
sqlQuery.AddScalar("NumeroCase", NHibernateUtil.String);
sqlQuery.AddScalar("QuantiteRepartieCorrigee", NHibernateUtil.Int32);
sqlQuery.AddScalar("NbreAppoints", NHibernateUtil.Int32);
sqlQuery.AddScalar("NbrePaquetsComplets", NHibernateUtil.Int32);
sqlQuery.AddScalar("NbrePaquetsOuverts", NHibernateUtil.Int32);
sqlQuery.AddScalar("TotalExemplaires", NHibernateUtil.Int32);
sqlQuery.AddScalar("IndiceSouchage", NHibernateUtil.String);

// paramètres d'entrée
sqlQuery.SetParameter("numeroDepot", ContexteLogistique.GetContexte().Utilisateur.CodeNumeroPermanentDepot, NHibernateUtil.String);
sqlQuery.SetParameter("codeReseau", ContexteLogistique.GetContexte().Utilisateur.CodeReseauDepot, NHibernateUtil.Character);
sqlQuery.SetParameter("datePreparation", dateParution, Int32Date.Type);
sqlQuery.SetParameter("codeTitre", codeTitre, NHibernateUtil.String);
sqlQuery.SetParameter("numeroParution", noParution, NHibernateUtil.String);
sqlQuery.SetParameter("suffixeParution", suffixe, NHibernateUtil.String);
sqlQuery.SetParameter("codeEdition", codeEdition, NHibernateUtil.String);
sqlQuery.SetParameter("typePreparation", typePreparation, NHibernateUtil.String);

// liste de cases
IList<ElementDST302> cases = sqlQuery.List<ElementDST302>();


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2008 12:05 pm 
Newbie

Joined: Tue Jan 16, 2007 4:44 am
Posts: 5
Hi,

Thanks for this answer but the question is for an HQL query, not an SQL.


In fact, there is probably a bug because
- writing the " AS " keyword in the SELECT clause of an HQL causes an exception like ", expected in SELECT"
- not giving any alias in the SELECT of an HQL causes nhibernate to send a null argument to the method TransformTuple of AliasToBeanResultTransformer and that's why you have the null reference exception.


As a quick work around, i created my own ResultTransfomer wich works without alias in the SELECT. Here is the code :

Code:
       
        public class PropertyInjecterResultTransformer : IResultTransformer
        {
            private Type resultType;
            private List<string> properties = null;

            public PropertyInjecterResultTransformer(Type type, ICollection<string> properties)
                : base()
            {
                resultType = type;
                this.properties = CollectionUtil.ToList(properties);
            }

            //the order of the field in your HQL query SELECT must be the same as the order of the properties given to me
            public object TransformTuple(object[] tuple, string[] aliases)
            {
                object result = Activator.CreateInstance(resultType);

                for (int i = 0; i < properties.Count; i++)
                {
                    ReflectionUtil.Set(result, properties[i], tuple[i]);
                }
                return result;
            }

            public IList TransformList(IList collection)
            {
                return collection;
            }

        }



To use this transformer, you need to give the property list like this :

Code:
                    query.SetResultTransformer(new PropertyInjecterResultTransformer(
                        typeof(CustomerDTO),
                        new string[] {
                            "Name",                           
                            "CityName",
                            "TotalPurchases"
                        }));



I hope this will help.

Cya.

_________________
=====
Nhibernate rox !

here is my (empty) blog


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.