0

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();
andrerav
  • 404
  • 5
  • 14
  • Before someone invests a lot of time into this; I have found the root cause. MySQL 5.7 introduced some changes that were simply not compatible with NHibernate.Spatial.MySQL. I have written a conceptual fix and will have this committed and published on NuGet fairly quick. When that happens I will add an answer to this post with instructions on how to use the new dialect. – andrerav Apr 25 '16 at 23:49

1 Answers1

0

This problem was due to lack of support for MySQL 5.7 in NHibernate.Spatial.MySQL. I have added a new MySQL57SpatialDialect in a prerelease version of NHibernate.Spatial.MySQL, which fixes this problem.

andrerav
  • 404
  • 5
  • 14