1

我通常不使用 NHibernate 2.2,所以我有几个问题无法找到很好的解释。我有 2 个表:点和文件。

声明为 C# 类:

[Serializable]
    public class PPoint
    {
        public PPoint()
        {
        }

        public virtual int PPointID { get; set; }
        public virtual int Position { get; set; }
        public virtual int ImportID { get { if (ImportFile == null) return -1; else return ImportFile.ImportID; } }
        public virtual PPImportFile ImportFile { get; set; }

        public virtual int Name{ get; set; }
        //more properties
    }

[Serializable]
public class PPImportFile
{
    public PPImportFile()
    {
        Points = new List<PPoint>();
    }

    private static Repositories.PPointRepository ppr; 

    public virtual int ImportID { get; set; }
    public virtual string FileName{ get; set; }
    public virtual DateTime CreateDate { get; set; }

    public virtual IList<PPoint> Points { get; set; }

    public virtual int PointCount { get { return Points.Count; } }
}

我的映射是:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="WebGPS.Data"
                   namespace="WebGPS.Data.Domain">

<id name="PPointID" column ="PPointID" type="System.Int32" unsaved-value="-1">
    <generator class="assigned"></generator>
</id>
<property name="Position" />
<!--<property name="ImportID" />-->
<many-to-one name="ImportFile" column="ImportID" update="true" insert="true" not-null="true" cascade="all-delete-orphan"  />

    <property name="Name" />
  </class>

</hibernate-mapping>

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                       assembly="WebGPS.Data"
                       namespace="WebGPS.Data.Domain">

  <class name="PPImportFile" lazy="true">
    <id name="ImportID" column ="ImportID" type="System.Int32" unsaved-value="-1">
        <generator class="assigned"></generator>
    </id>
    <property name="FailoPav" />
    <property name="ImportoData" />
    <property name="ImportavoVart" />

    <list name="Points" table="PPoint" cascade="all" lazy="true">
      <key column="PPointID" not-null="true" unique="true"/>
      <index column="Position"/>
      <one-to-many class="PPoint"/>
    </list>
  </class>

</hibernate-mapping>

用点插入新文件的代码:

PPImportFile file = new PPImportFile();

int newPointID = pp.GetNewPontID(); // Get next Point ID from repository
do
{
   PPoint pt = new PPoint();
   pt.PPointID = newPointID;
   pt.ImportFile = file;
   // assign other properties

   file.Points.Add(pt);
   newPointID +=1;
} while (!eof)

if (fileNew)
    repository.Add(file); 
else
    repository.Update(file); // get an error here

我已将 Position 列添加到 Point 表和类中,现在尝试在加载现有记录并获取旧错误后获取点集合: 未能延迟初始化角色集合:PPImportFile.Points

4

1 回答 1

1

有两个问题。首先总是将集合映射为接口:可用类型在此处列出6.1。持久性集合 (例如IList, Iesi.Collections.ISet

其次,索引列。C# IList 可以实现(List 是一个很好的例子)作为一个包含一个值多次的集合(同一个 Point 可以被添加不止一次)。允许 .NET 区分此列表中的项目的唯一信息是索引。这就是 NHibernate 所支持的

因此,如果您的映射将是:

public virtual IList<PPoint> Points { get; set; }

并且您想将其映射为<list>表应具有“ForeignKey”列和从零开始的“Index”列。该索引列将由 NHibernate 管理,并用IList索引值填充。在此处阅读更多内容19.5.1。分类

建议:

如果无法更改数据库,请更改映射以避免索引列。

IList<PPoint>可以映射为存在一些性能问题(阅读链接) ,<bag>但它会为您工作。

使用Iesi.Collections.Generic.ISet<T><set>映射。Set 将确保每个值只添加到集合中一次。

更多关于<list>这里http://ayende.com/blog/4043/nhibernate-mapping-list

于 2013-02-05T04:57:16.010 回答