Re: Patch for QueryTranslator Invalid Cast to object array

Felix Gartsman <[email protected]>
Newsgroups gmane.comp.windows.dotnet.nhibernate.devel
Message-ID <[email protected]>
Isn’t this similar to Hibernate issues:
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2463
http://opensource.atlassian.com/projects/hibernate/browse/HHH-2525
They are also present in NH, and they prevent caching queries with
IResultTransformer – the caching thinks it gets a tuple, but it’s actually
the transformed object.


From: [email protected]
[mailto:[email protected]] On Behalf Of
Ayende Rahien
Sent: Wednesday, June 11, 2008 1:52 PM
To: the NHibernate development list
Subject: Re: [NHibernate-development] Patch for QueryTranslator Invalid Cast
to object array

The problem is with your IResultTransformer class.
You need to create a new object[] { my, domain, entities}, instead of new
MyEntity[] { my, domain, entities}
On Wed, Jun 11, 2008 at 11:51 AM, Stefan Simroth <[email protected]> wrote:
Hello NHibernate developers,

there seems to be an issue with a hard cast in the QueryTranslator class,
line 1452 (trunk) - resp. line 1288 (1.2.1) ...

The hard cast in method GetResultList to an object[] fails in the case of my
company's product, when using an IResultTransformer.

I chose to use an IResultTransformer implementation for Criteria's and
Query's to be able to set a required property. It seems an appropriate way
to me and my tests (and the running app) ensure that it works.

However, I had to patch the QueryTranslator at the locations mentioned
above, because it threw an invalid cast exception (stacktrace below) -
because the IList of my objects could not be casted to an object[].
Debugging it, showed me that the "results" list contained my domain objects,
but it could not be casted to an object[]. It was a collection of my domain
types and my domain types are no object[]. Now, some time passed by since
then, and our product works fine with the patch, so unfortunately I forgot
the exact reason, but I'd guess, I used the generic Query signature and so
the results list was a generic list that cannot be casted to object[]. But
also ArrayList or Hashtable cannot be casted to object[]...

A little experiment, gave me the compiler error:

Cannot convert type
'System.Collections.Generic.List<ConsoleApplication1.Project>' to 'object[]'

class Program
{
       static void Main(string[] args)
       {
               List<Project> projects = new List<Project>();
               object[] objs = (object[]) projects;
       }

       class Project { }
}

=> same thing doesn't work with ArrayList or Hashtable ...


----
The code snippet from QueryTranslator in question:


       for (int i = 0; i < results.Count; i++)
       {
               object[] row = (object[]) results[i];
               results[i] = holderInstantiator.Instantiate(row);
       }

----
Patch - with a "safe" cast:

       object[] row = results[i] as object[];
       if (row != null)
       {
               results[i] = holderInstantiator.Instantiate(row);
       }



The included patch is for the NHibernate 2.0 trunk.
The same problem exists in earlier versions, namely 1.2.

I might be totally missing the root of the problem, but please excuse this,
as I am not familiar with the NHibernate internals, but tried to apply a
patch at the right place to enable the very useful feature of the
IResultTransformer to our case - and maybe to others in future.


Here is the stacktrace:  (version 1.2.1)

  at NHibernate.Hql.Classic.QueryTranslator.GetResultList(IList results,
IResultTransformer resultTransformer) in
d:\dev\yap\nhibernate\trunk\src\NHibernate\Hql\Classic\QueryTranslator.cs:li
ne 1291
  at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor
session, QueryParameters queryParameters) in
d:\dev\yap\nhibernate\trunk\src\NHibernate\Loader\Loader.cs:line 1744
  at NHibernate.Loader.Loader.List(ISessionImplementor session,
QueryParameters queryParameters, ISet querySpaces, IType[] resultTypes) in
d:\dev\yap\nhibernate\trunk\src\NHibernate\Loader\Loader.cs:line 1738
  at NHibernate.Hql.Classic.QueryTranslator.List(ISessionImplementor
session, QueryParameters queryParameters) in
d:\dev\yap\nhibernate\trunk\src\NHibernate\Hql\Classic\QueryTranslator.cs:li
ne 1124
  at NHibernate.Impl.SessionImpl.Find(String query, QueryParameters
parameters, IList results) in
d:\dev\yap\nhibernate\trunk\src\NHibernate\Impl\SessionImpl.cs:line 1763



Hope to could have helped a bit ...
Nevertheless, big thanx for such a great software!

... best regards,

Stefan


-- 
ifu Hamburg - material flows and software

ifu Institut fuer Umweltinformatik Hamburg GmbH
Grosse Bergstrasse 219, 22767 Hamburg, Germany
Managing Director: Jan Hedemann, Commercial Register: Hamburg, HRB 52629
www.ifu.com - www.umberto.de - www.sabento.com - www.e-sankey.com
e!Sankey - software for easy drawing of Sankey diagrams.
Visit http://www.e-sankey.com

Index: QueryTranslator.cs
===================================================================
--- QueryTranslator.cs  (revision 3539)
+++ QueryTranslator.cs  (working copy)
@@ -1449,8 +1449,11 @@
                       {
                               for (int i = 0; i < results.Count; i++)
                               {
-                                       object[] row = (object[])
results[i];
-                                       results[i] =
holderInstantiator.Instantiate(row);
+                                       object[] row = results[i] as
object[];
+                                       if (row != null)
+                                       {
+                                               results[i] =
holderInstantiator.Instantiate(row);
+                                       }
                               }

                               if (holderConstructor == null &&
resultTransformer != null)

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Nhibernate-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/nhibernate-development



-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
Nhibernate-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/nhibernate-development
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.