我通常不使用 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