3

I have the following XML (.hbm):

<property name="Geometry" column="the_geom">
   <type name="NHibernate.Spatial.Type.GeometryType,NHibernate.Spatial">
      <param name="subtype">MULTIPOLYGON</param>
      <param name="srid">-1</param>
   </type>
</property>

It´s using Nhibernate Spatial type... How can I map that property using ClassMap (Fluent Nhibernate) ?

Thanks

1 Answer 1

3

Well, I've not used NHibernate Spatial, but I browsed through the code and it looks like GeometryType inherits from IUserType so you should be able to use it with .CustomTypeIs<>

For example:

Map(x => x.Geometry, "the_geom").CustomTypeIs<GeometryType>();

Unless it happens automagically, that probably won't get you your param elements though. I'm not sure of a truly nice way to do this but you can always add an XML alteration like so:

Map(x => x.Geometry, "the_geom").AddAlteration(p => p.AddElement("type")
    .WithAtt("name", "NHibernate.Spatial.Type.GeometryType,NHibernate.Spatial")
        .AddElement("param")
            .WithAtt("name", "subtype")
            .WithText("MULTIPOLYGON")
        .ParentNode
        .AddElement("param")
            .WithAtt("name", "srid")
            .WithText("-1")
    );

Note that to get the WithText functionality, you'll have to add an extension for XmlElement like so (WithAtt and AddElement are extensions in the FluentNHibernate.Mapping namespace):

public static class XmlExtensions
{
    public static XmlElement WithText(this XmlElement element, string text)
    {
        element.InnerText = text;
        return element;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

When trying to use your code with Fluent NHibernate 1.0 I'm unable to find the AddAlteration function within the PropertyPart type. Is your code working with version 1.0 of Fluent NHibernate?
No, this no longer works. In fact, WithAtt was removed not long after I made this post, if memory serves. You can still set the custom type with Map(x => ...).CustomType<Type>(), but I don't think you'll be able to get the param attribute/elements in there. I'd suggest posting to the FNH group at: groups.google.com/group/fluent-nhibernate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.