NHibernate – Find not returning a result?

I was having an issue with NHibernate a while back (which I fixed a while back :-P) – anyway – here’s the problem and a hopeful fix if anyone else has come across the same thing. Maybe it might be something to go into the NHibernate base… Although I noticed with the new 1.2.0 Alpha release Find is being deprecated so maybe this comes a little late but anyway… It might be something you also want to patch in as I did (into the 1.0.2 release and the 1.2.0 Alpha releases – it still exists in the 1.2.0 Alpha release) 🙂

 The problem was when I did a search over a entity and the field I was searching on contained both full stops(.) and commas(,), even though I could see the entity in the database, it wouldn’t return it – it would always say the entity didn’t exist (if I escaped it – rightly so – the fields wouldn’t match) but if I didn’t escape it it would start throwing exceptions – arrrhhh. For example, say I was looking for a company with the name “BigCompany, United.” using Find – I would get nothing (escaped) or would blow up (unescaped). What was going on? Time to bust out the source code 😀

 Here’s the query at fault:

array = (ArrayList)session.Find(string.Format(“from Business.Mappings.Company as company” +
      ” where company.Name = ‘{0}'”,
       Utils.GetSafeQueryParameter(comapnyName)));

The problem exists down in the parsing of the find string. When it comes across commas and full stops it tried (if it wasnt escaped) to cast it to a class in the solution – if it failed to cast it would blow up. Trying to modify the source as little as possible, so everything else will stay the same, (theres some code down in here that really needs to be tidied up by the NHibernate crew – sorry guys) I did the following:

In the class ReflectHelper, in the method GetConstantValue I moved the try up 2 lines – so the exception will get caught and then the code that calls this will fall back on the “else” action (to pass the string through as a parameter – what we want). Here’s the modified method:

public static object GetConstantValue( string typeName, string fieldName )
{
    try
    {
        System.Type clazz = System.Type.GetType( typeName, false );

        if (clazz == null)
        {
            return null;
        }
        else
        {
            return clazz.GetField(fieldName).GetValue(null);
        }
    }
    catch( Exception )
    {
        return null;
    }
}

Drop that guy in and away you go 🙂 The problem is that with any extension dll’s they are compiled against a signed version of the NHibernate dll 😦 (which I dont have) To get around this I either had to recompile the extension packages from source – or – using reflector dissassemble the dll back to source so I could recompile it – or – in one case (where reflector was throwing hissy fits on some generics that were being used) dump the dll out to msil – delete the signed part form the msil code (its near the top of the file) and then reassemble it – turned out to be a little mission but fixed a bug that was really bugging me… Was good to get it solved 🙂

If anyone finds this useful then let me know/forward on a link to the Nhibernate guys… 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: