nhibernate:在 ManyToOne 属性中使用复合 ID 的一部分

发布于 2024-11-07 17:31:50 字数 3376 浏览 1 评论 0原文

我有一个像这样的表结构:

Table entity
(
    otherEntity_id  int  // primarykey
    id              int  // primarykey

    parent_id       int
)

和类

public class Entity
{
    public OtherEntity Other { get; set; }
    // simple int to discriminate different Entity for the same OtherEntity
    public int Id { get; set; }

    public Entity Parent { get; set; }
}

是否可以将其映射到类实体? (如果是的话,如何)

保存时会抛出以下错误,因为 dbcommand 中没有足够的列,一个被使用了两次:

        CompositeId()
            .KeyReference(e => e.Other, "otherEntity_id")
            .KeyProperty(e => e.Id, "id")

        References(e => e.Parent).Columns("otherEntity_id", "parent_id");

使用 xml 或 Fluent 并不重要。

编辑: 没有参考映射没有错误,有参考映射跟随错误 (我多次出现此错误,每次当我的值多于映射的列时):

System.ArgumentOutOfRangeException: System.ArgumentOutOfRangeException : Der Index lag außerhalb des Bereichs. Er muss nicht negativ und kleiner als die Auflistung sein.
Parametername: index
   bei System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
   bei System.ThrowHelper.ThrowArgumentOutOfRangeException()
   bei System.Collections.Generic.List`1.get_Item(Int32 index)
   bei Npgsql.NpgsqlParameterCollection.get_Item(Int32 index)
   bei Npgsql.NpgsqlParameterCollection.GetParameter(Int32 index)
   bei System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item(Int32 index)
   bei NHibernate.Type.Int16Type.Set(IDbCommand rs, Object value, Int32 index)
   bei NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index)
   bei NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, ISessionImplementor session)
   bei NHibernate.Type.ComponentType.NullSafeSet(IDbCommand st, Object value, Int32 begin, ISessionImplementor session)
   bei NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index)
   bei NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session)
   bei NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session)
   bei NHibernate.Action.EntityInsertAction.Execute()
   bei NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
   bei NHibernate.Engine.ActionQueue.ExecuteActions(IList list)
   bei NHibernate.Engine.ActionQueue.ExecuteActions()
   bei NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)
   bei NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
   bei NHibernate.Impl.SessionImpl.Flush()

测试代码

        var e1 = new Entity { Id = 2, Other = other };
        var e2 = new Entity { Id = 3, Other = other, Parent = e1 };

        Session.Save(e1);
        Session.Save(e2);
        Session.Flush();    // throws here
        Session.Clear();

        var key = new Entity { Id = e2.Id, Other = e2.Other };

        var loaded = Session.Get<Entity>(key);

编辑:

如果不可能,请有人告诉我吗?

I have a tablestructure like this:

Table entity
(
    otherEntity_id  int  // primarykey
    id              int  // primarykey

    parent_id       int
)

and class

public class Entity
{
    public OtherEntity Other { get; set; }
    // simple int to discriminate different Entity for the same OtherEntity
    public int Id { get; set; }

    public Entity Parent { get; set; }
}

is it possible to map this to the class Entity? (if yes how)

the following throws on save, because there are not enough columns in the dbcommand, one is used twice:

        CompositeId()
            .KeyReference(e => e.Other, "otherEntity_id")
            .KeyProperty(e => e.Id, "id")

        References(e => e.Parent).Columns("otherEntity_id", "parent_id");

It doesnt matter using xml or fluent.

Edit:
without the reference mapped no error, with reference mapped following error
(i had this error several times, everytime when i have more values than mapped columns):

System.ArgumentOutOfRangeException: System.ArgumentOutOfRangeException : Der Index lag außerhalb des Bereichs. Er muss nicht negativ und kleiner als die Auflistung sein.
Parametername: index
   bei System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
   bei System.ThrowHelper.ThrowArgumentOutOfRangeException()
   bei System.Collections.Generic.List`1.get_Item(Int32 index)
   bei Npgsql.NpgsqlParameterCollection.get_Item(Int32 index)
   bei Npgsql.NpgsqlParameterCollection.GetParameter(Int32 index)
   bei System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item(Int32 index)
   bei NHibernate.Type.Int16Type.Set(IDbCommand rs, Object value, Int32 index)
   bei NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index)
   bei NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, ISessionImplementor session)
   bei NHibernate.Type.ComponentType.NullSafeSet(IDbCommand st, Object value, Int32 begin, ISessionImplementor session)
   bei NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index)
   bei NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session)
   bei NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session)
   bei NHibernate.Action.EntityInsertAction.Execute()
   bei NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
   bei NHibernate.Engine.ActionQueue.ExecuteActions(IList list)
   bei NHibernate.Engine.ActionQueue.ExecuteActions()
   bei NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)
   bei NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
   bei NHibernate.Impl.SessionImpl.Flush()

testcode

        var e1 = new Entity { Id = 2, Other = other };
        var e2 = new Entity { Id = 3, Other = other, Parent = e1 };

        Session.Save(e1);
        Session.Save(e2);
        Session.Flush();    // throws here
        Session.Clear();

        var key = new Entity { Id = e2.Id, Other = e2.Other };

        var loaded = Session.Get<Entity>(key);

Edit:

if it's not possible could please someone tell me?

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

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

发布评论

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

评论(2

染火枫林 2024-11-14 17:31:50

如果您两次引用同一列(otherEntity_id),我见过这样的映射以避免此类错误:

References(e => e.Parent).Columns("otherEntity_id", "parent_id")
    .Not.Update()
    .Not.Insert();

此外,当您遇到问题时,显示您遇到的完整错误消息总是有帮助的。

If you are referencing the same column above twice (otherEntity_id) I've seen mappings like this to avoid this sort of error:

References(e => e.Parent).Columns("otherEntity_id", "parent_id")
    .Not.Update()
    .Not.Insert();

Also when you are running into a problem it is always helpful to show the full error message you are running into.

标点 2024-11-14 17:31:50

我仍然没有找到解决方案并将我的模型映射为:

public class Entity
{
    public OtherEntity Other { get; set; }
    // simple int to discriminate different Entity for the same OtherEntity
    public int Id { get; set; }

    public int ParentId { get; set; }
}

I still haven't found a solution and mapped changed my model to:

public class Entity
{
    public OtherEntity Other { get; set; }
    // simple int to discriminate different Entity for the same OtherEntity
    public int Id { get; set; }

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