NHibernate Forge
The NHibernate community site.

Configuration and mapping

Page Details

First published by:
Ricardo Stuven
on 09-26-2008
Last revision by:
Ricardo Stuven
on 10-07-2008
This page has not yet been rated
Article
Comments (2)
History (4)

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.

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>
PropertyValue
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.

Recent Comments

By: Noam Gal Posted on 11-01-2008 13:44

Is there any plan on creating a Geography class for NHibernate?

By: santoshamb Posted on 10-18-2008 5:19

For anybody (like me) new to NHibernate and NHibernate.Spatial, you'll need to import

NHibernate.Spatial.Mapping

in order to build with a reference to the SpatialAuxiliaryDatabaseObject

View More
Powered by Community Server (Commercial Edition), by Telligent Systems