简体   繁体   English

NHibernate <one-to-many>

[英]NHibernate <one-to-many>

How can I map these two classes? 如何映射这两个类?

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

Accually I have this xml: 准确地说,我有这个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>

But I get an error: 但是我得到一个错误:

An association from the table Reading refers to an unmapped class 表Reading中的关联是指未映射的类

There are few improvements I would made to make it running 我将对其进行一些改进以使其运行

1) C# 1)C#

If we map collections, they MUST be represented as interfaces, so NHibernate will be able to inject proxies... own implementation 如果我们映射集合,则它们必须表示为接口,因此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 2)hbm

For mapping collection I would use bag (until NHibernate 4 is released with native use of System stuff, we would need to use iesi collection ... so <bag> is good enough now) . 对于映射集合,我将使用bag (直到NHibernate 4随System的本机使用发布为止,我们将需要使用iesi collection ...因此<bag>现在已经足够好了)

Also if propery name and column are same, we do not have to mention column. 同样,如果属性名称和列相同,则不必提及列。

Finally, all mappings are strongly LAZY now. 最后,所有映射现在都非常懒惰。 This is simply the way which to go 这只是要走的路

<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) be sure that both mapping are Emebedded Resources . 3)确保两个映射都是嵌入式资源 If one of these is not found... not embedded... the exception as in the question is thrown... Also, both files should/must have suffix .hbm.xml 如果未找到其中之一...未嵌入...抛出问题中的异常...另外,两个文件都应该/必须带有后缀.hbm.xml

More reading: 更多阅读:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM