NHibernate <one-to-many>

本文关键字:one-to-many gt lt NHibernate | 更新日期: 2023-09-27 18:15:14

如何映射这两个类?

public class LDAVehicle 
{
    public virtual int VehicleId { get; set; }
    public virtual string ChassisSeries { get; set; }
    public virtual string ChassisNumber { get; set; }
    public virtual List<LDAReading> Readings { get; set; }
}
public class LDAReading 
{
    public virtual int ReadingId { get; set; }
    public virtual DateTime IncomingDate { get; set; }
    public virtual DateTime ReadoutDate { get; set; }
    public virtual string Sender { get; set; }
    public virtual LDAVehicle Vehicle { get; set; }
}

实际上我有这个xml:

<class name="LDAVehicle" table="Vehicle" schema="dbo" lazy="false">
    <id name="VehicleId">
        <column name="VehicleId" />
        <generator class="native"></generator>
    </id>
    <property name="ChassisSeries" column="ChassisSeries" not-null="false" />
    <property name="ChassisNumber" column="ChassisNumber" not-null="false" />
    <set name="Readings" table="Reading" fetch="join">
        <key foreign-key="VehicleId" />
        <one-to-many class="LDAReading" />
    </set>
</class>
<class name="LDAReading" table="Reading" schema="dbo" lazy="false">
    <id name="ReadingId">
        <column name="ReadingId" />
        <generator class="native"></generator>
    </id>
    <property name="IncomingDate" column="IncomingDate" not-null="true" />
    <property name="ReadoutDate" column="ReadoutDate" not-null="true" />
    <property name="Sender" column="Sender" not-null="false" />
    <many-to-one name="Vehicle" class="LDAVehicle" fetch="select" column="VehicleId" />
</class>

但是我得到一个错误:

来自表Reading的关联指向未映射的类

NHibernate <one-to-many>

我想做一些改进来让它运行

1) c#

如果我们映射集合,它们必须被表示为接口,所以NHibernate将能够注入代理…自己的实现

public class LDAVehicle 
{
    public virtual int VehicleId { get; set; }
    public virtual string ChassisSeries { get; set; }
    public virtual string ChassisNumber { get; set; }
    //public virtual List<LDAReading> Readings { get; set; }
    public virtual IList<LDAReading> Readings { get; set; }
}
public class LDAReading 
{
    public virtual int ReadingId { get; set; }
    public virtual DateTime IncomingDate { get; set; }
    public virtual DateTime ReadoutDate { get; set; }
    public virtual string Sender { get; set; }
    public virtual LDAVehicle Vehicle { get; set; }
}

2) hbm

对于映射集合,我将使用bag (直到NHibernate 4发布了本地使用System的东西,我们将需要使用iesi集合…所以<bag>现在足够好了)

同样,如果属性名和列相同,我们不必提到列。

最后,现在所有映射都是强LAZY的。这就是该走的路

<class name="LDAVehicle" table="Vehicle" schema="dbo" lazy="true"> // should be lazy
    <id name="VehicleId" column="VehicleId" generator="native" />  // simple id def
    <property name="ChassisSeries" not-null="false" /> // name is enough
    <property name="ChassisNumber" not-null="false" /> // if column is the same
    // IList and <bag> go work together
    // also profit from lazy loading, 
    //  batch size fetching and inverse mapping
    <bag name="Readings" lazy="true" inverse="true" 
                         batch-size="25" cascade="all-delete-orphan"> 
        <key column="VehicleId" />
        <one-to-many class="LDAReading" />
    </bag>
</class>
<class name="LDAReading" table="Reading" schema="dbo" lazy="true">
    <id name="ReadingId" generator="native" />
    <property name="IncomingDate" not-null="true" />
    <property name="ReadoutDate"  not-null="true" />
    <property name="Sender"       not-null="false" />
    <many-to-one name="Vehicle" class="LDAVehicle" column="VehicleId" />
</class>

3)确保两个映射都是Emebedded Resources。如果没有找到其中一个…不是嵌入式…抛出问题中的异常…另外,两个文件都应该/必须有后缀.hbm.xml

更多阅读:

  • 逆映射
  • Ayende - NHibernate是懒惰的,只是生活与它
  • 批大小获取