代码之家  ›  专栏  ›  技术社区  ›  Jaime Febres

NHibernate中的映射问题

  •  2
  • Jaime Febres  · 技术社区  · 17 年前

    public class Customer
    {
        public virtual int CustomerID { get; protected set; }
        public virtual string AccountNumber { get; protected set; }
        public virtual string CustomerType { get; set; }
        public virtual int TerritoryID { get; set; }
        public virtual SalesTerritory Territory { get; protected set; }
    }
    

    使用Fluent NHibernate绘制地图如下:

        public class CustomerMapper : ClassMap<Customer>
    {
        private const string schema = "Sales";
        public CustomerMapper()
        {
            SchemaIs(schema);
            Id(x => x.CustomerID);
            Map(x => x.CustomerType).WithLengthOf(1).Not.Nullable();
            Map(x => x.TerritoryID).Not.Nullable();
    
            Map(x => x.AccountNumber).ReadOnly()
                .Update(false)
                .Insert(false)
                .Generated(Generated.Insert);
    
            References<SalesTerritory>(x => x.Territory).TheColumnNameIs(ReflectionHelper.GetProperty<Customer>(x => x.TerritoryID).Name).LazyLoad().Update(false).Insert(false);
        }
    }
    

    更新 生成的映射文件中属性的属性。

    <?xml version="1.0" encoding="utf-8"?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true" assembly="Model" namespace="Model" schema="Sales">
        <class name="Customer" table="`Customer`" xmlns="urn:nhibernate-mapping-2.2">
            <id name="CustomerID" column="CustomerID" type="Int32">
                <generator class="identity" />
            </id>
            <property name="AccountNumber" column="AccountNumber" insert="false" update="false" generated="insert" length="100" type="String">
                <column name="AccountNumber" />
            </property>
            <property name="TerritoryID" column="TerritoryID" not-null="true" type="Int32">
                <column name="TerritoryID" />
            </property>
            <property name="CustomerType" column="CustomerType" not-null="true" length="1" type="String">
                <column name="CustomerType" />
            </property>
            <many-to-one lazy="proxy" update="false" insert="false" name="Territory" column="TerritoryID" />
        </class>
    </hibernate-mapping>
    

    我的问题是,当一个新的客户实例保存到数据库中时,客户实例的Territory属性为null。
    如何使此属性在保存到数据库后包含正确的值? 生成
    或者我的客户类有什么问题吗?
    提前谢谢。

    2 回复  |  直到 17 年前
        1
  •  2
  •   gcores    17 年前

    更合理的映射如下:

    public class CustomerMapper : ClassMap<Customer>
    {
        private const string schema = "Sales";
        public CustomerMapper()
        {
            SchemaIs(schema);
            Id(x => x.CustomerID);
            Map(x => x.CustomerType).WithLengthOf(1).Not.Nullable();
            Map(x => x.AccountNumber).ReadOnly()
               .Generated(Generated.Insert);
            References(x => x.Territory).LazyLoad();
        }
    }
    

    • 这算是一个映射的SalesTerritory类。
    • 如果使用Insert(false)和Update(false),则这些属性将不会保存到数据库中。ReadOnly()同时执行插入(false)和更新(false)。
    • 客户类型完全可以是一个枚举。

    这或多或少是个主意(航空规则)。

        2
  •  0
  •   Frederik Gheysels    17 年前

    属地财产与属地财产有什么关系? 在多对一映射中,在映射Territory属性时,不必指定Territory的类型吗?(销售区域)。 为什么要将同一列映射到2?

    推荐文章