私は通常NHibernate2.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列を追加しましたが、既存のレコードを読み込んだ後にポイントのコレクションを取得しようとすると、古いエラーが発生 します:ロールのコレクションを遅延初期化できませんでした:PPImportFile.Points