Logo

NHibernate

The object-relational mapper for .NET

Spatial

This page is converted from the old nhforge.org Wiki. First published by: Ricardo Stuven on 09-26-2008, Last revision by: Ricardo Stuven on 07-21-2009

Configuration and mapping

Dialect Configuration:

  • In the NHibernate section of your configuration file add the following entry:
<property name="dialect">NHibernate.Spatial.Dialect.SomeSpatialDialect,SomeSpatialAssembly</property>
<add key="dialect" value="NHibernate.Spatial.Dialect.SomeSpatialDialect,SomeSpatialAssembly" />


Replace SomeSpatialDialect by the spatial dialect to be used (eg. MsSql2008SpatialDialect.) and SomeSpatialAssembly by the assembly name where it is located (eg. NHibernate.Spatial.MsSql2008)

Schema Generation Configuration:

  • Declarative: Use of <database-object> element is not supported.
  • Programmatic:    
    Configuration cfg = new Configuration();
    // Your configuration code (eg. cfg.Configure(); )
    cfg.AddAuxiliaryDatabaseObject(new SpatialAuxiliaryDatabaseObject(cfg));
    
    


Then you can use a SchemaExport object or the hibernate.hbm2ddl.auto configuration property for schema generation.

ActiveRecord Schema Generation Configuration:

You will need to add the SpatialAuxiliaryDatabaseObject in the Application_OnStart:

 using NHibernate.Cfg;
using NHibernate.Spatial.Mapping;

public void Application_OnStart()
{
foreach (Configuration cfg in ActiveRecordMediator.GetSessionFactoryHolder().GetAllConfigurations())
{
cfg.AddAuxiliaryDatabaseObject(new SpatialAuxiliaryDatabaseObject(cfg));
Metadata.AddMapping(cfg, MetadataClass.GeometryColumn);
Metadata.AddMapping(cfg, MetadataClass.SpatialReferenceSystem);
}
}

You can then generate the database schema in the usual way.

 

Metadata Mappings Configuration:

There are two classes available for metadata mapping in the NHibernate.Spatial.Metadata namespace: GeometryColumn and SpatialReferenceSystem. You can add them selectively:

Configuration cfg = new Configuration();
// Your configuration code (eg. cfg.Configure(); )
Metadata.AddMapping(cfg, MetadataClass.GeometryColumn);
Metadata.AddMapping(cfg, MetadataClass.SpatialReferenceSystem);


Then you can use them as normal mapped classes. For example, you could execute the following HQL query:

select srs.WellKnownText from SpatialReferenceSystem as srs where srs.SRID=4326


WARNING: DO NOT add metadata class mappings when using the SchemaExport utility. You could lose all contents of metadata tables.

NHibernate Mapping:

  • In the XML mappings file, map all properties as usual but for geometry columns use the following entry:
<property name="Geometry" column="the_geom" type="NHibernate.Spatial.Type.GeometryType, NHibernate.Spatial" />

 

  • Optionally, for schema generation and default values setting, you can use type parameters. The available parameters are srid and subtype. For example:
<property name="Geometry" column="the_geom">
<type name="NHibernate.Spatial.Type.GeometryType, NHibernate.Spatial">
<param name="srid">4326</param>
<param name="subtype">POLYGON</param>
</type>
</property>

 

  • In the mapped class, declare the corresponding property:
private GeoAPI.Geometries.IGeometry _geometry; 
public virtual GeoAPI.Geometries.IGeometry Geometry
{
get
{
return this._geometry;
}
set
{
this._geometry = value;
}
}

 

ActiveRecord Mapping:

 

private GeoAPI.Geometries.IGeometry _geometry; 

[Property("the_geom", ColumnType = "NHibernate.Spatial.Type.GeometryType, NHibernate.Spatial")]
public virtual GeoAPI.Geometries.IGeometry Geometry
{
get
{
return this._geometry;
}
set
{
this._geometry = value;
}
}


NOTE: Type parameters (see NHibernate mapping above) are not supported in ActiveRecord.

ActiveWriter Mapping:

ActiveWriter is a tool to visually model entities and relationships, and generate ActiveRecord classes or NHibernate mapping files. Since Preview 3, it supports custom types, so we can use the GeometryType / IGeometry pair..

Example usage:

  • Drag & drop a MsSqlSpatial table from Server Explorer/Data connections.
  • In the model, select "the_geom" property.
  • In the properties window, enter the following values:

<tbody> <tbody> </tbody> </tbody> <tbody> </tbody>

Property Value
Column Type Custom
Custom Column Type NHibernate.Spatial.Type.GeometryType, NHibernate.Spatial
Custom Member Type GeoAPI.Geometries.IGeometry

NOTE: Type parameters (see NHibernate mapping above) are not supported in ActiveRecord, so it is in ActiveWriter.

© NHibernate Community 2024