-->
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.  [ 5 posts ] 
Author Message
 Post subject: Bypassing Mapping document failures
PostPosted: Wed Oct 10, 2007 7:19 am 
Newbie

Joined: Fri Oct 05, 2007 4:28 pm
Posts: 3
NHibernate version: 1.2

Ok, our project has a DLL that contains all of our mapping files as embedded resources. Occasionally somebody will make a change to a class file that will cause the mapping document to fail. Since this does not break a build it makes it to our dev / qa environments with the mapping broken.

We are using a CI approach to our project but the CI servers currently do not have any data connectivitiy nor have we had the time to setup Rhino Mocks so we have been unable to include unit tests in our build to catch these.

My question is this. Is there a way to configure NHibernate to not stop when there is a mapping document error? So if I have say 30 mapping documents but 1 is broken it won't stop the whole thing.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 10, 2007 9:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
One of my test fixtures does a query for every single mapped class. If one of the mapping has an error, the test fails. It is not fool-proof, but it has helped us so far. The second test below checks that all mapped classes are serializable. Adapt the code below to you own needs:

Code:
using NHibernate;
using NUnit.Framework;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace PatientSys.Data
{
    [TestFixture]
    public class TestMappingFixture : FixtureBase
    {
        [Test]
        public void LoadAllMappedClasses()
        {
            Type[] typesToSkip = new Type[] {
                typeof(PatientSys.Core.Domain.MissingDataRecord),
                typeof(PatientSys.Core.Domain.CaseProgram.ExtendedInformation),
            };
            Dictionary<Type, Exception> typesWithError = new Dictionary<Type,Exception>();

            IDictionary allClasses = this.NhSession.SessionFactory.GetAllClassMetadata();
            foreach (Type mappedClassType in allClasses.Keys)
            {
                try
                {
                    // Skip a few classes because they do something funky and do
                    // not actually map to a table.
                    bool skip = false;
                    foreach (Type typeToSkip in typesToSkip)
                    {
                        if (mappedClassType.Equals(typeToSkip))
                        {
                            skip = true;
                            break;
                        }
                    }
                    if (skip) continue;

                    // TODO: Fix the Note class hierarchy
                    //if (typeof(PatientSys.Core.Domain.Note).IsAssignableFrom(mappedClassType)) continue;

                    NhSession.Clear();
                    ICriteria crit = NhSession.CreateCriteria(mappedClassType)
                        .SetMaxResults(10);

                    IList list = crit.List();
                }
                catch (Exception ex)
                {
                    typesWithError.Add(mappedClassType, ex);
                }
            }

            if (typesWithError.Count > 0)
            {
                StringBuilder sb = new StringBuilder("The following mapped types have mapping error:\n");
                foreach (KeyValuePair<Type, Exception> kvPair in typesWithError)
                {
                    string errLine = string.Format("\t{0} -- {1}", kvPair.Key.FullName, kvPair.Value.Message);
                    sb.AppendLine(errLine);
                }

                Assert.Fail(sb.ToString());
            }
        }

        [Test]
        public void CheckAllMappedClassesAreSerializable()
        {
            List<Type> typesWithError = new List<Type>();

            IDictionary allClasses = this.NhSession.SessionFactory.GetAllClassMetadata();
            foreach (Type mappedClassType in allClasses.Keys)
            {
                if ((int)(mappedClassType.Attributes & System.Reflection.TypeAttributes.Serializable) == 0)
                {
                    typesWithError.Add(mappedClassType);
                }
            }

            if (typesWithError.Count > 0)
            {
                StringBuilder sb = new StringBuilder("The following mapped types are not serializable:\n");
                foreach (Type type in typesWithError)
                {
                    string errLine = string.Format("\t{0}", type.FullName);
                    sb.AppendLine(errLine);
                }

                Assert.Fail(sb.ToString());
            }
        }
    }
}

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 11, 2007 7:45 am 
Newbie

Joined: Fri Oct 05, 2007 4:28 pm
Posts: 3
Thanks for this info. I'll adapt as I need to and try to add to our build process.

However is there a way to configure nhibernate to still continue on an error?

What we have run into is that while the unit tests may work on the machine somebody else may have modified code and it gets into the build. Then it breaks because the unit tests were not run to verify this. We are in the process of breaking up our unit tests to exclude database tests as our build servers do not have DB connectivitiy so right now we do not have unit tests in our automated build. So we are on a kind of honor system.

Again thanks for this code.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 11, 2007 9:46 am 
Hibernate Team
Hibernate Team

Joined: Tue Jun 13, 2006 11:29 pm
Posts: 315
Location: Calgary, Alberta, Canada
You are welcome.

I am not aware of a way to configure NHibernate in a way that it will continue with a mapping error.

_________________
Karl Chu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 11, 2007 7:30 pm 
Regular
Regular

Joined: Wed Oct 25, 2006 10:51 pm
Posts: 71
karlchu wrote:
I am not aware of a way to configure NHibernate in a way that it will continue with a mapping error.


I think before you go hacking this in you really have to consider changing your build so it does break and get your CI machine a database connection. Otherwise it's not really CI is it?

For our build, we create a test for every single mapped class, which just creates, saves and retrieves.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.