I am attempting to create a simple demo solution using NHibernate.Spatial.MySQL (version 4.0.4.4001). The solution is available here: https://github.com/andrerav/NHibernate.Spatial.MySql.Demo
The mapping seems to work at least for inserts -- the DemoDataImport project is able to read the GeoJSON files, and insert geometries into the database, and I can verify the results using MySQL Workbench.
However, if I query the data, geometries always show up with null values. Furthermore, if I perform a query such as this:
var municipalities = SessionManager.Session.Query<Municipality>()
.Where(m => m.Area.Within(county.Area)).ToList();
I get an exception that says "No persister for: GeoAPI.Geometries.IGeometry".
Any ideas what could be wrong?
To run the solution, first create a mysql database (mysql 5.7 or newer) called mysqldemo with username/password mysqldemo/mysqldemo. The DemoDataImport project will dump geojson data into the database, and the DemoQueryUtil project can be used to execute queries.
Mapping:
public class Municipality
{
public virtual int Id { get; set; }
public virtual County County { get; set; }
public virtual string Name { get; set; }
public virtual int MunicipalityNo { get; set; }
public virtual IGeometry Area { get; set; }
}
public class MunicipalityMap : ClassMap<Municipality>
{
public MunicipalityMap()
{
ImportType<IGeometry>();
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.MunicipalityNo);
Map(x => x.Area).CustomType<MySQLGeometryType>();
References(x => x.County).Nullable();
}
}
public class County
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual int CountyNo { get; set; }
public virtual IGeometry Area { get; set; }
public virtual List<Municipality> Municipalities { get; set; }
}
public class CountyMap : ClassMap<County>
{
public CountyMap()
{
ImportType<IGeometry>();
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.CountyNo);
Map(x => x.Area).CustomType<MySQLGeometryType>();
}
}
Configuration:
public static void Configure(bool generateTables = false)
{
var cfg = Fluently.Configure()
.Database(FluentNHibernate.Cfg.Db.MySQLConfiguration.Standard
.ConnectionString(c => c.FromConnectionStringWithKey("MySQL"))
.Driver<MySqlDataDriver>()
.ShowSql()
.Dialect<MySQLSpatialDialect>())
.Mappings(x => x.FluentMappings.AddFromAssemblyOf<MunicipalityMap>())
.BuildConfiguration();
cfg.AddAuxiliaryDatabaseObject(new SpatialAuxiliaryDatabaseObject(cfg));
if (generateTables)
{
var exporter = new SchemaExport(cfg);
exporter.Drop(false, true);
exporter.Create(true, true);
}
SessionManager.SessionFactory = cfg.BuildSessionFactory();
}
Example query:
var county = SessionManager.Session.Query<County>().First();