代码之家  ›  专栏  ›  技术社区  ›  Khaja Minhajuddin

在fluent nhibernate映射中同时使用join和component抛出“找不到属性异常的getter”

  •  2
  • Khaja Minhajuddin  · 技术社区  · 15 年前

    在fluent nhibernate映射中一起使用join和组件抛出“找不到属性异常的getter”。这是我的密码

    using FluentNHibernate.Mapping;
    
    namespace FnhTest {
        public class CustomerMap : ClassMap<Customer> {
            public CustomerMap() {
                Id(x => x.Id).GeneratedBy.Identity();
                Map(x => x.Name);
    
                Join("BillingInfo", m =>
                                   {
                                       m.KeyColumn("CustomerId");
                                       m.Component(x => x.BillingInfo, c =>
                                                                       {
                                                                           c.Map(y => y.AccountNumber);
                                                                           c.Map(y => y.Address);
                                                                       });
                                   });
            }
        }
    
        public class BillingInfo {
            public virtual string AccountNumber { get; set; }
            public virtual string Address { get; set; }
        }
    
        public class Customer {
            public virtual int Id { get; set; }
            public virtual string Name { get; set; }
    
            public virtual BillingInfo BillingInfo { get; set; }
        }
    }
    

    这是我的数据库结构=>

    Customers:
      Id (int)
      Name (varchar 50)
    BillingInfo:
      Id (int)
      AccountNumber (varchar 50)
      Address (varchar 50)
      CustomerId (int) (Foriegn Key to the Customers Id)
    

    FluentNHibernate为此设置生成正确的映射,但出于某种原因,它抛出了一个错误。下面给出了映射和错误

    Mapping:

    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
      <class xmlns="urn:nhibernate-mapping-2.2" name="FnhTest.Customer, FnhTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Customer`">
        <id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <column name="Id" />
          <generator class="identity" />
        </id>
        <property name="Name" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
          <column name="Name" />
        </property>
        <join table="BillingInfo">
          <key>
            <column name="CustomerId" />
          </key>
          <component name="BillingInfo" insert="true" update="true" optimistic-lock="true">
            <property name="AccountNumber" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
              <column name="AccountNumber" />
            </property>
            <property name="Address" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
              <column name="Address" />
            </property>
          </component>
        </join>
      </class>
    </hibernate-mapping>
    

    Error:

    TestCase 'M:FnhTest.Program.Main(System.String[])'
    failed: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.
    
      * Database was not configured through Database method.
    
        FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.
    
          * Database was not configured through Database method.
         ---> NHibernate.PropertyNotFoundException: Could not find a getter for property 'AccountNumber' in class 'FnhTest.Customer'
        at NHibernate.Properties.BasicPropertyAccessor.GetGetter(Type type, String propertyName)
        at NHibernate.Tuple.Component.PocoComponentTuplizer.BuildGetter(Component component, Property prop)
        at NHibernate.Tuple.Component.AbstractComponentTuplizer..ctor(Component component)
        at NHibernate.Tuple.Component.PocoComponentTuplizer..ctor(Component component)
        at NHibernate.Tuple.Component.ComponentEntityModeToTuplizerMapping..ctor(Component component)
        at NHibernate.Tuple.Component.ComponentMetamodel..ctor(Component component)
        at NHibernate.Mapping.Component.BuildType()
        at NHibernate.Mapping.Component.get_Type()
        at NHibernate.Mapping.SimpleValue.IsValid(IMapping mapping)
        at NHibernate.Mapping.PersistentClass.Validate(IMapping mapping)
        at NHibernate.Mapping.RootClass.Validate(IMapping mapping)
        at NHibernate.Cfg.Configuration.Validate()
        at NHibernate.Cfg.Configuration.BuildSessionFactory()
        d:\Builds\FluentNH\src\FluentNHibernate\Cfg\FluentConfiguration.cs(93,0): at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory()
           --- End of inner exception stack trace ---
        d:\Builds\FluentNH\src\FluentNHibernate\Cfg\FluentConfiguration.cs(100,0): at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory()
        D:\repositories\core\playground\minhajuddin\FnhTest\FnhTest\Program.cs(8,0): at FnhTest.Program.Main(String[] args)
    
          * Database was not configured through Database method.
    
    
    0 passed, 1 failed, 0 skipped, took 6.46 seconds (Ad hoc).
    

    我在网上搜索过,但是找不到任何有用的东西,任何帮助都会非常感谢:)

    编辑: 好吧,我找不到用流利的nhibernate来做这件事的方法,我正在用托克尔发布的任何内容作为答案。但是,这不是我的意图。不管怎样。

    2 回复  |  直到 15 年前
        1
  •  2
  •   Torkel    15 年前

    在我看来,billinginfo不是一个组件,而是一个实体。

    如果您将BillingInfo映射为一个实体,那么您可以将它从客户映射为一个关联。

    <many-to-one name="BillingInfo" property-ref="CustomerId" cascade="none"/>
    

    属性ref很重要,因为您不想在billinginfo id上加入,而是在customerid上加入,但这要求您将customerid属性添加到billinginfo类中。

        2
  •  0
  •   Tom Bushell    15 年前

    如果这是一个愚蠢的观察,请原谅我,但你不需要在billinginfo中有一个ID成员吗?