问题
I need to read a postgis geometry column as wkt using nHibernate. I know I could use nHibernate Spatial, but that is not an option in my case.
I have seen this post: Best way to map a hidden property in NHibernate (fluent) where the wkt is stored in another column and he uses triggers to update the actual geometry. However the project owners do not want to have an extra column for this purpose.
I found this thread as well: Use PostGIS columns transparently in Hibernate, but no has provided an answer to that.
I believe I need to write a custom type, but I would really need someone to point me to right direction. I have not worked with Postgresql/postgis earlier.
Thnx!
回答1:
If you don't want an additional column you could create a view that will that will pull the data from original table using a function and create a instead of trigger on the view to update the real table.
回答2:
I solved the problem by using nHibernates sql-interceptor. Not the most robust solution but this way I can have different interceptors for PostGis and Oracle.
I also looked into nHibernate.Spatial and their geometry types. But I did not want to use NetTopologySuite and therefore I should have needed to code a lot more.
Here is part of the code as an example:
public class PostGisGeometrySqlInterceptor : IInterceptor
{
...
/// <summary>
/// Modifies all the select statements with geometry so that
/// xxx.geometry -> ST_AsText(xxx.geometry)
/// </summary>
/// <param name="sql">The original sql string.</param>
/// <returns>The modified sql string.</returns>
/// <remarks>
/// All the geometry fields must be called 'geometry'.
/// Works only for one geometry in the sql string.
/// </remarks>
public virtual SqlString OnPrepareStatement(SqlString sql)
{
if (sql.StartsWithCaseInsensitive("SELECT") && sql.ToString().Contains("geometry"))
{
var indexOfGeometry = sql.IndexOfCaseInsensitive("geometry");
var indexOfSpace = sql.Substring(0, indexOfGeometry).LastIndexOfCaseInsensitive(" ") + 1;
var oldValue = sql.ToString(indexOfSpace, indexOfGeometry - indexOfSpace + "geometry".Length);
var newValue = string.Format("ST_AsText({0})", oldValue);
sql = sql.Replace(oldValue, newValue);
}
else if(...)
...
return sql;
}
来源:https://stackoverflow.com/questions/16437375/how-to-map-postgis-geometry-column-as-wkt-with-nhibernate-not-using-nhibernate-s