ActiveRecord 和 NHibernate Spatial

发布于 2024-11-18 07:10:07 字数 3692 浏览 4 评论 0原文

我在使用 ActiveRecord 时遇到一些问题。

好吧,一切都很好,但有时会起作用。并非总是如此。

当我尝试导航到包含空间实体的项目中引用的 MVC 页面(只有一个空间实体 - 并且该实体没有空间类型)时,我收到此异常。

{“已声明 GeometryType 列,但未配置空间方言”}

已正确配置方言。我尝试用两种方式配置它:Xml 和 InPlace。

这是我的启动方法:

    public static void StartActiveRecord()
    {
        IDictionary<string,string> hash = new Dictionary<string,string>();

        hash.Add("isWeb", "true");
        hash.Add("connection.driver_class","NHibernate.Driver.NpgsqlDriver");
        hash.Add("connection.connection_string","Server=localhost;Port=5432;database=nhiber;User ID=postgres;Password=pass;");
hash.Add("connection.provider","NHibernate.Connection.DriverConnectionProvider");
            hash.Add("dialect","NHibernate.Spatial.Dialect.PostGisDialect,NHibernate.Spatial.PostGis");
            hash.Add("proxyfactory.factory_class","NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");

        InPlaceConfigurationSource source = new InPlaceConfigurationSource();
        source.Add(typeof(ActiveRecordBase), hash);
        ActiveRecordStarter.Initialize(source, GetActiveRecordTypes());

        foreach (Configuration cfg in ActiveRecordMediator.GetSessionFactoryHolder().GetAllConfigurations())
        {
            cfg.AddAuxiliaryDatabaseObject(new SpatialAuxiliaryDatabaseObject(cfg));
            //Metadata.AddMapping(cfg, MetadataClass.GeometryColumn);
            //Metadata.AddMapping(cfg, MetadataClass.SpatialReferenceSystem);
        }
    }

而这是我的启动方法,在Global.asax中

    protected void Application_Start()
    {
        Ignition.StartActiveRecord();

        AreaRegistration.RegisterAllAreas();

        RegisterRoutes(RouteTable.Routes);
    }

有时会出现这个错误。有时终止开发服务器会使其正常,但几步后就会再次崩溃。

帮助!

编辑:我将映射添加到此信息和其他一些信息

当存在方言时,Global.asax 上的 Ignition.StartActiveRecord() 中会出现错误。当没有方言时,它会在 ActiveRecordStarter.Initialize(); 中出错。

可以肯定的是,下面映射的这个对象是整个组件中唯一的空间感知对象。

public class Complaint:ActiveRecordBase<Complaint>
{

[PrimaryKey(Column="complaint_id",Generator=PrimaryKeyType.Sequence,SequenceName="complaint_seq")]
        public virtual int ComplaintId { get; set; }

        [Property(Column="date_of_complaint",NotNull=true)]
        public virtual DateTime DateOfComplaint { get; set; }

        [Property(Column="description",Length=256,NotNull=true)]
        public virtual string Description { get; set; }

        [Property(Column="complaint_status",NotNull=true,Default="1")]
        public cityzenComplaintStatus Status { get; set; }

        [BelongsTo(Column = "complaint_type_id")]
        public ComplaintType Type { get; set; }

        [Property("the_geom", ColumnType = "NHibernate.Spatial.Type.GeometryType, NHibernate.Spatial")]
        public virtual IGeometry Geometry { get; set; }

        [OneToOne(ForeignKey="official_answer_id")]
        public virtual OfficialAnswer CityAnswer { get; set; }

        [BelongsTo("user_id", Fetch = FetchEnum.Select, Lazy = FetchWhen.OnInvoke, Update = false, NotNull = true)]
        public virtual CityzenUser User { get; set; }

        [HasMany(typeof(Vote),Table="vote",ColumnKey="complaint_id",RelationType=RelationType.Set,Inverse=true)]
        public virtual IList<Vote> Votes { get; set; }

        [HasMany(typeof(Comment),Table="comment",ColumnKey="complaint_id",RelationType=RelationType.Set,Inverse=true)]
        public virtual IList<Comment> Comments { get; set; }

        [Property(Column = "deleted", Default = "false", NotNull = true)]
        public virtual bool Deleted { get; set; }
}

I'm having some problems with ActiveRecord.

Well everything works fine, but it's working sometimes. Not ALL the time.

When I try to navigate to MVC page that is referenced in a project that contains a spatial entity (theres just one spatial entity - and this entity does not have a spatial type) I get this exception.

{"A GeometryType column has been declared, but there is no spatial dialect configured"}

There is a dialect correctly configured. I've tried to configurate it in two ways: Xml and InPlace.

This is my startup method:

    public static void StartActiveRecord()
    {
        IDictionary<string,string> hash = new Dictionary<string,string>();

        hash.Add("isWeb", "true");
        hash.Add("connection.driver_class","NHibernate.Driver.NpgsqlDriver");
        hash.Add("connection.connection_string","Server=localhost;Port=5432;database=nhiber;User ID=postgres;Password=pass;");
hash.Add("connection.provider","NHibernate.Connection.DriverConnectionProvider");
            hash.Add("dialect","NHibernate.Spatial.Dialect.PostGisDialect,NHibernate.Spatial.PostGis");
            hash.Add("proxyfactory.factory_class","NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle");

        InPlaceConfigurationSource source = new InPlaceConfigurationSource();
        source.Add(typeof(ActiveRecordBase), hash);
        ActiveRecordStarter.Initialize(source, GetActiveRecordTypes());

        foreach (Configuration cfg in ActiveRecordMediator.GetSessionFactoryHolder().GetAllConfigurations())
        {
            cfg.AddAuxiliaryDatabaseObject(new SpatialAuxiliaryDatabaseObject(cfg));
            //Metadata.AddMapping(cfg, MetadataClass.GeometryColumn);
            //Metadata.AddMapping(cfg, MetadataClass.SpatialReferenceSystem);
        }
    }

And this is my Startup method, in Global.asax

    protected void Application_Start()
    {
        Ignition.StartActiveRecord();

        AreaRegistration.RegisterAllAreas();

        RegisterRoutes(RouteTable.Routes);
    }

This error occurs sometimes. Killing the dev server sometimes makes it ok, but only to crash again a few steps later.

HELP!

EDIT: I'm adding the mappings to this and some other info

When there is a dialect, this errors out in Ignition.StartActiveRecord() on Global.asax. When there is no dialect it errors out in ActiveRecordStarter.Initialize();

Just to be sure, this object mapped below is the ONLY spatial aware object in the entire assembly.

public class Complaint:ActiveRecordBase<Complaint>
{

[PrimaryKey(Column="complaint_id",Generator=PrimaryKeyType.Sequence,SequenceName="complaint_seq")]
        public virtual int ComplaintId { get; set; }

        [Property(Column="date_of_complaint",NotNull=true)]
        public virtual DateTime DateOfComplaint { get; set; }

        [Property(Column="description",Length=256,NotNull=true)]
        public virtual string Description { get; set; }

        [Property(Column="complaint_status",NotNull=true,Default="1")]
        public cityzenComplaintStatus Status { get; set; }

        [BelongsTo(Column = "complaint_type_id")]
        public ComplaintType Type { get; set; }

        [Property("the_geom", ColumnType = "NHibernate.Spatial.Type.GeometryType, NHibernate.Spatial")]
        public virtual IGeometry Geometry { get; set; }

        [OneToOne(ForeignKey="official_answer_id")]
        public virtual OfficialAnswer CityAnswer { get; set; }

        [BelongsTo("user_id", Fetch = FetchEnum.Select, Lazy = FetchWhen.OnInvoke, Update = false, NotNull = true)]
        public virtual CityzenUser User { get; set; }

        [HasMany(typeof(Vote),Table="vote",ColumnKey="complaint_id",RelationType=RelationType.Set,Inverse=true)]
        public virtual IList<Vote> Votes { get; set; }

        [HasMany(typeof(Comment),Table="comment",ColumnKey="complaint_id",RelationType=RelationType.Set,Inverse=true)]
        public virtual IList<Comment> Comments { get; set; }

        [Property(Column = "deleted", Default = "false", NotNull = true)]
        public virtual bool Deleted { get; set; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风夜微凉 2024-11-25 07:10:07

您的配置对我来说看起来是正确的,但无论出于何种原因,每当 NH 实例化 GeometryType 实例时,它都无法找到您配置使用的方言,以便它知道您需要初始化什么类型的 IGeometry (在您的情况下是 PostGisGeometryType )。

您可以做的临时解决方法是像这样声明您的成员变量:

[Property("the_geom", ColumnType = "NHibernate.Spatial.Type.PostGisGeometryType, NHibernate.Spatial.PostGis")]
public virtual IGeometry Geometry { get; set; }

如果我发现其他任何内容,我将发布回此处。

Your configuration looks right to me but for whatever reason whenever NH instantiates an instance of GeometryType it cannot find the dialect you are configured to use so that it knows what type of IGeometry you need to initialize (PostGisGeometryType in your case).

What you may be able to do for a temporary workaround is to declare your member variable like this:

[Property("the_geom", ColumnType = "NHibernate.Spatial.Type.PostGisGeometryType, NHibernate.Spatial.PostGis")]
public virtual IGeometry Geometry { get; set; }

If I find anything else out I will post back here.

多彩岁月 2024-11-25 07:10:07

这是对的吗?
hash.Add("方言","NHibernate.Spatial.Dialect.PostGisDialect,NHibernate.Spatial.PostGis");
不应该是这样的:
hash.Add("方言","NHibernate.Spatial.Dialect.PostGisDialect,NHibernate.Spatial");

看一下: http://nhibernate.info/doc/spatial/configuration- and-mapping.html

is this right?
hash.Add("dialect","NHibernate.Spatial.Dialect.PostGisDialect,NHibernate.Spatial.PostGis");
shouldn't be like this:
hash.Add("dialect","NHibernate.Spatial.Dialect.PostGisDialect,NHibernate.Spatial");

?

take a peek at: http://nhibernate.info/doc/spatial/configuration-and-mapping.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文