Using NHibernate.Spatial

Topics: Customizing Orchard, Troubleshooting
Sep 18, 2011 at 9:55 PM
Edited Sep 19, 2011 at 11:55 AM

Hello, I am trying to use NHibernate.Spatial with orchard. I am neither an expert in NHiberante nor Fluent NHibernate but it seems that NHibernate.Spatial does not support Fluent NHibernate out of the box, please correct me If I am wrong.

So to bridge the gap between Fluent NHibernate and NHibernate.Spatial, I think I need to do two things

  1. Create a custom ColumnCreateCommand  to be used in a subclass of DataMigrationImpl like the following:
    • table.Column<string>("SomeStringColumn"); table.TableCommands.Add( new CreateGeomCommand<Point>("SomeGeomCommand"))
    • There will be some extra plumbing to plug the CreateGeomCommand into the  IDataMigrationInterpreter but I will think about that later
  2. Make Fluent NHibernate know how to map the geometry columns  for this I created a custom IDataServicesProvider (and registered it with autofac via xml) that is very similar to the default one for sql server except that
    • In GetPersistanceConfigurer it will use the geom dialect using :
    var persistence = MsSqlConfiguration.MsSql2008.Dialect<NHibernate.Spatial.Dialect.MsSql2008GeometryDialect>();
    
    and adds an alteration to the AutoPersistenceModel.
    This alteration will look for all properties in records and if the property is a of geometry type, it will map it using the following code
    (model object is of type AutoPersistenceModel
          model.Override<T>(mapping => {
                //mapping.Map(x => x.Location).CustomType(typeof(NHibernate.Spatial.Type.GeometryType));
                var geomProps = typeof(T).GetProperties()
                                .Where(p => typeof(GeoAPI.Geometries.IGeometry).IsAssignableFrom(p.PropertyType));
                foreach (var prop in geomProps)
                {
                    ParameterExpression xParam = Expression.Parameter(typeof(T), "x");
                    var access = Expression.MakeMemberAccess(xParam, prop);
                    Expression<Func<T, object>> lambda = Expression.Lambda<Func<T, object>>(access, xParam);
                    mapping.Map(lambda).CustomType(typeof(NHibernate.Spatial.Type.GeometryType));
                }
                
            });

    this code was inspired from a stackoverflow answer on the following link http://stackoverflow.com/questions/1495169/nhibernate-spatial-and-sql-2008-geography-type-how-to-configure

    however this code gives me the following error. Any ideas ?



 

Server Error in '/' Application.

The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'joined-subclass' in namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'meta, subselect, cache, synchronize, comment, tuplizer, id, composite-id' in namespace 'urn:nhibernate-mapping-2.2'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Xml.Schema.XmlSchemaValidationException: The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'joined-subclass' in namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'meta, subselect, cache, synchronize, comment, tuplizer, id, composite-id' in namespace 'urn:nhibernate-mapping-2.2'.

Source Error:

 

Line 31:                 alt.Add(new SpatialRecordAlteration(parameters.RecordDescriptors)));
Line 32: 
Line 33:             return Fluently.Configure()
Line 34:                 .Database(database)
Line 35:                 .Mappings(m => m.AutoMappings.Add(persistenceModel))


Source File: G:\Shady\workspace\Orchard\Playground\orchard\src\Orchard.Web\Modules\Shady.OrchardSpatial\Data\Providers\SpatialSqlServerDataServicesProvider.cs Line: 33

Stack Trace:

[XmlSchemaValidationException: The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'joined-subclass' in namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'meta, subselect, cache, synchronize, comment, tuplizer, id, composite-id' in namespace 'urn:nhibernate-mapping-2.2'.]

[MappingException: (XmlDocument)(3,6): XML validation error: The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'joined-subclass' in namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'meta, subselect, cache, synchronize, comment, tuplizer, id, composite-id' in namespace 'urn:nhibernate-mapping-2.2'.]
   NHibernate.Cfg.Configuration.LogAndThrow(Exception exception) +88
   NHibernate.Cfg.Configuration.ValidationHandler(Object o, ValidationEventArgs args) +390
   System.Xml.Schema.XmlSchemaValidator.SendValidationEvent(ValidationEventHandler eventHandler, Object sender, XmlSchemaValidationException e, XmlSeverityType severity) +54
   System.Xml.Schema.XmlSchemaValidator.ElementValidationError(XmlQualifiedName name, ValidationState context, ValidationEventHandler eventHandler, Object sender, String sourceUri, Int32 lineNo, Int32 linePos, XmlSchemaSet schemaSet) +761
   System.Xml.Schema.XmlSchemaValidator.ValidateElementContext(XmlQualifiedName elementName, Boolean& invalidElementInContext) +4024504
   System.Xml.Schema.XmlSchemaValidator.ValidateElement(String localName, String namespaceUri, XmlSchemaInfo schemaInfo, String xsiType, String xsiNil, String xsiSchemaLocation, String xsiNoNamespaceSchemaLocation) +142
   System.Xml.XsdValidatingReader.ProcessElementEvent() +161
   System.Xml.XsdValidatingReader.ProcessReaderEvent() +53
   System.Xml.XsdValidatingReader.Read() +49
   System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace) +57
   System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc) +20
   System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) +131
   System.Xml.XmlDocument.Load(XmlReader reader) +114
   NHibernate.Cfg.Configuration.LoadMappingDocument(XmlReader hbmReader, String name) +219
   NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String name) +45
   NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name) +149
   NHibernate.Cfg.Configuration.AddDocument(XmlDocument doc, String name) +279
   NHibernate.Cfg.Configuration.AddDocument(XmlDocument doc) +43
   FluentNHibernate.PersistenceModel.Configure(Configuration cfg) in E:\BitBucket\FluentGit\fluent-nhibernate\src\FluentNHibernate\PersistenceModel.cs:235
   FluentNHibernate.Automapping.AutoPersistenceModel.Configure(Configuration configuration) in E:\BitBucket\FluentGit\fluent-nhibernate\src\FluentNHibernate\Automapping\AutoPersistenceModel.cs:126
   FluentNHibernate.Cfg.AutoMappingsContainer.Apply(Configuration cfg) in E:\BitBucket\FluentGit\fluent-nhibernate\src\FluentNHibernate\Cfg\AutoMappingsContainer.cs:71
   FluentNHibernate.Cfg.MappingConfiguration.Apply(Configuration cfg) in E:\BitBucket\FluentGit\fluent-nhibernate\src\FluentNHibernate\Cfg\MappingConfiguration.cs:64
   FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() in E:\BitBucket\FluentGit\fluent-nhibernate\src\FluentNHibernate\Cfg\FluentConfiguration.cs:110

[FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

]
   FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() in E:\BitBucket\FluentGit\fluent-nhibernate\src\FluentNHibernate\Cfg\FluentConfiguration.cs:119
   Shady.OrchardSpatial.Providers.SpatialSqlServerDataServicesProvider.BuildConfiguration(SessionFactoryParameters parameters) in G:\Shady\workspace\Orchard\Playground\orchard\src\Orchard.Web\Modules\Shady.OrchardSpatial\Data\Providers\SpatialSqlServerDataServicesProvider.cs:33
   Orchard.Data.<>c__DisplayClass3.<BuildConfiguration>b__2() in g:\Shady\workspace\Orchard\Playground\orchard\src\Orchard\Data\SessionFactoryHolder.cs:85
   Orchard.Data.SessionConfigurationCache.GetConfiguration(Func`1 builder) in g:\Shady\workspace\Orchard\Playground\orchard\src\Orchard\Data\SessionConfigurationCache.cs:51
   Orchard.Data.SessionFactoryHolder.BuildConfiguration() in g:\Shady\workspace\Orchard\Playground\orchard\src\Orchard\Data\SessionFactoryHolder.cs:84
   Orchard.Data.SessionFactoryHolder.GetConfiguration() in g:\Shady\workspace\Orchard\Playground\orchard\src\Orchard\Data\SessionFactoryHolder.cs:62
   Orchard.Data.SessionFactoryHolder.BuildSessionFactory() in g:\Shady\workspace\Orchard\Playground\orchard\src\Orchard\Data\SessionFactoryHolder.cs:74
   Orchard.Data.SessionFactoryHolder.GetSessionFactory() in g:\Shady\workspace\Orchard\Playground\orchard\src\Orchard\Data\SessionFactoryHolder.cs:53
   Orchard.Data.SessionLocator.For(Type entityType) in g:\Shady\workspace\Orchard\Playground\orchard\src\Orchard\Data\SessionLocator.cs:29
   Orchard.Data.Migration.Interpreters.DefaultDataMigrationInterpreter..ctor(ShellSettings shellSettings, ISessionLocator sessionLocator, IEnumerable`1 commandInterpreters, ISessionFactoryHolder sessionFactoryHolder, IReportsCoordinator reportsCoordinator) in g:\Shady\workspace\Orchard\Playground\orchard\src\Orchard\Data\Migration\Interpreters\DefaultDataMigrationInterpreter.cs:36

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle._InvokeConstructor(IRuntimeMethodInfo method, Object[] args, SignatureStruct& signature, RuntimeType declaringType) +0
   System.RuntimeMethodHandle.InvokeConstructor(IRuntimeMethodInfo method, Object[] args, SignatureStruct signature, RuntimeType declaringType) +15
   System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +281
   System.Reflection.ConstructorInfo.Invoke(Object[] parameters) +17
   Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate() +215
   Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters) +416
   Autofac.Core.Resolving.ComponentActivation.Activate(IEnumerable`1 parameters) +91
   Autofac.Core.Resolving.<>c__DisplayClass1.<Execute>b__0() +39
   Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func`1 creator) +151
   Autofac.Core.Resolving.ComponentActivation.Execute(IEnumerable`1 parameters) +315
   Autofac.Core.Resolving.ResolveOperation.Resolve(ISharingLifetimeScope activationScope, IComponentRegistration registration, IEnumerable`1 parameters) +248
   Autofac.Core.Resolving.ComponentActivation.Resolve(IComponentRegistration registration, IEnumerable`1 parameters) +61
   Autofac.Core.Activators.Reflection.<>c__DisplayClass2.<CanSupplyValue>b__0() +68
   Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate() +157
   Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters) +416
   Autofac.Core.Resolving.ComponentActivation.Activate(IEnumerable`1 parameters) +91
   Autofac.Core.Resolving.<>c__DisplayClass1.<Execute>b__0() +39
   Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func`1 creator) +151
   Autofac.Core.Resolving.ComponentActivation.Execute(IEnumerable`1 parameters) +315
   Autofac.Core.Resolving.ResolveOperation.Resolve(ISharingLifetimeScope activationScope, IComponentRegistration registration, IEnumerable`1 parameters) +248
   Autofac.Core.Resolving.ComponentActivation.Resolve(IComponentRegistration registration, IEnumerable`1 parameters) +61
   Autofac.Core.Activators.Reflection.<>c__DisplayClass2.<CanSupplyValue>b__0() +68
   Autofac.Core.Activators.Reflection.ConstructorParameterBinding.Instantiate() +157
   Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters) +416
   Autofac.Core.Resolving.ComponentActivation.Activate(IEnumerable`1 parameters) +91
   Autofac.Core.Resolving.<>c__DisplayClass1.<Execute>b__0() +39
   Autofac.Core.Lifetime.LifetimeScope.GetOrCreateAndShare(Guid id, Func`1 creator) +151
   Autofac.Core.Resolving.ComponentActivation.Execute(IEnumerable`1 parameters) +315
   Autofac.Core.Resolving.ResolveOperation.Resolve(ISharingLifetimeScope activationScope, IComponentRegistration registration, IEnumerable`1 parameters) +248
   Autofac.Core.Resolving.ComponentActivation.Resolve(IComponentRegistration registration, IEnumerable`1 parameters) +61
   Autofac.Features.Collections.<>c__DisplayClass6.<RegistrationsFor>b__1(IComponentRegistration cr) +52
   System.Linq.WhereSelectArrayIterator`2.MoveNext() +85
   System.Linq.Buffer`1..ctor(IEnumerable`1 source) +325
   System.Linq.Enumerable.ToArray(IEnumerable`1 source) +78
   Autofac.Features.Collections.<>c__DisplayClass4.<RegistrationsFor>b__0(IComponentContext c, IEnumerable`1 p) +233
   Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters) +96
   Autofac.Core.Resolving.ComponentActivation.Activate(IEnumerable`1 parameters) +91
   Autofac.Core.Resolving.ComponentActivation.Execute(IEnumerable`1 parameters) +213
   Autofac.Core.Resolving.ResolveOperation.Resolve(ISharingLifetimeScope activationScope, IComponentRegistration registration, IEnumerable`1 parameters) +248
   Autofac.Core.Resolving.ComponentActivation.Resolve(IComponentRegistration registration, IEnumerable`1 parameters) +61
   Autofac.Core.Registration.<>c__DisplayClass5.<RegistrationsFor>b__2(IComponentContext c, IEnumerable`1 p) +49
   Autofac.Builder.<>c__DisplayClass1`1.<ForDelegate>b__0(IComponentContext c, IEnumerable`1 p) +51
   Autofac.Core.Activators.Delegate.DelegateActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters) +96
   Autofac.Core.Resolving.ComponentActivation.Activate(IEnumerable`1 parameters) +91
   Autofac.Core.Resolving.ComponentActivation.Execute(IEnumerable`1 parameters) +213
   Autofac.Core.Resolving.ResolveOperation.Resolve(ISharingLifetimeScope activationScope, IComponentRegistration registration, IEnumerable`1 parameters) +248
   Autofac.Core.Resolving.ResolveOperation.Resolve(IComponentRegistration registration, IEnumerable`1 parameters) +52
   Autofac.Core.Lifetime.LifetimeScope.Resolve(IComponentRegistration registration, IEnumerable`1 parameters) +137
   lambda_method(Closure ) +198
   Orchard.Events.<NotifyHandlers>d__0.MoveNext() in g:\Shady\workspace\Orchard\Playground\orchard\src\Orchard\Events\DefaultOrchardEventBus.cs:36
   System.Linq.Buffer`1..ctor(IEnumerable`1 source) +217
   System.Linq.Enumerable.ToArray(IEnumerable`1 source) +78
   Orchard.Events.DefaultOrchardEventBus.Notify(String messageName, IDictionary`2 eventData) in g:\Shady\workspace\Orchard\Playground\orchard\src\Orchard\Events\DefaultOrchardEventBus.cs:25
   Orchard.Events.EventsInterceptor.Intercept(IInvocation invocation) in g:\Shady\workspace\Orchard\Playground\orchard\src\Orchard\Events\EventsInterceptor.cs:24
   Castle.DynamicProxy.AbstractInvocation.Proceed() +184
   IOrchardShellEventsProxy939a96b92b8d491daad991ef47e05dee.Activated() +153
   Orchard.Environment.DefaultOrchardShell.Activate() in g:\Shady\workspace\Orchard\Playground\orchard\src\Orchard\Environment\DefaultOrchardShell.cs:40
   Orchard.Environment.DefaultOrchardHost.ActivateShell(ShellContext context) in g:\Shady\workspace\Orchard\Playground\orchard\src\Orchard\Environment\DefaultOrchardHost.cs:128
   Orchard.Environment.DefaultOrchardHost.<CreateAndActivate>b__4(ShellSettings settings) in g:\Shady\workspace\Orchard\Playground\orchard\src\Orchard\Environment\DefaultOrchardHost.cs:113
   System.Linq.WhereSelectArrayIterator`2.MoveNext() +85
   System.Linq.Buffer`1..ctor(IEnumerable`1 source) +217
   System.Linq.Enumerable.ToArray(IEnumerable`1 source) +78
   Orchard.Environment.DefaultOrchardHost.BuildCurrent() in g:\Shady\workspace\Orchard\Playground\orchard\src\Orchard\Environment\DefaultOrchardHost.cs:97
   Orchard.Environment.DefaultOrchardHost.BeginRequest() in g:\Shady\workspace\Orchard\Playground\orchard\src\Orchard\Environment\DefaultOrchardHost.cs:177
   Orchard.Environment.DefaultOrchardHost.Orchard.Environment.IOrchardHost.BeginRequest() in g:\Shady\workspace\Orchard\Playground\orchard\src\Orchard\Environment\DefaultOrchardHost.cs:74
   Orchard.Web.MvcApplication.HostBeginRequest(HttpApplication application, IOrchardHost host) in G:\Shady\workspace\Orchard\Playground\orchard\src\Orchard.Web\Global.asax.cs:38
   Orchard.WarmupStarter.Starter`1.OnBeginRequest(HttpApplication application) in g:\Shady\workspace\Orchard\Playground\orchard\src\Orchard.Startup\Starter.cs:68
   Orchard.Web.MvcApplication.Application_BeginRequest() in G:\Shady\workspace\Orchard\Playground\orchard\src\Orchard.Web\Global.asax.cs:29

[TargetInvocationException: Exception has been thrown by the target of an invocation.]
   System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner) +0
   System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner) +72
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) +335
   System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +28
   System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +19
   System.Web.Util.ArglessEventHandlerProxy.Callback(Object sender, EventArgs e) +92
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +218
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +120



Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.237