nhibernate:在ManyToOne属性中使用compositeId的一部分

时间:2011-05-18 16:26:52

标签: nhibernate fluent-nhibernate

我有这样的表结构:

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; }
}

是否可以将其映射到类Entity? (如果是的话)

以下命令保存,因为dbcommand中没有足够的列,其中一列使用了两次:

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

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

使用xml或流利无关紧要。

编辑: 没有引用映射没有错误,引用映射跟随错误 (每当我有比映射列更多的值时,我会多次出现此错误):

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);

编辑:

如果不可能请某人告诉我?

2 个答案:

答案 0 :(得分:0)

如果你引用上面两次相同的列(otherEntity_id)我已经看到这样的映射来避免这种错误:

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

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

答案 1 :(得分:0)

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

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; }
}