系列的抽象类和NHibernate

时间:2010-05-08 20:57:25

标签: nhibernate orm nhibernate-mapping

首先感谢你抽出时间来看看这个。

对于我正在研究的一个研究项目,我有一个复杂的设计(我已经给过)通过NHibernate持久保存到数据库。

以下是类层次结构的示例:

alt text http://img215.imageshack.us/img215/3998/transit.png

TransitStrategy,TransportationCompany和TransportationLocation都是抽象类。

我目前的XML配置是:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               assembly="Vacationizer"
               namespace="Vacationizer.Domain.Transit">

<class name="TransitStrategy">

    <id name="TransitStrategyId">
        <generator class="guid" />
    </id>

    <property name="Restrictions" />

    <joined-subclass name="Flight" table="Flight_TransitStrategy">

        <key column="TransitStrategyId" />

        <property name="DepartingAirport" />
        <property name="ArrivingAirport" />
        <property name="Airline" />
        <property name="FlightNumber" />
        <property name="FlightArrivalTime" />
        <property name="FlightDepartureTime" />

    </joined-subclass>

    <joined-subclass name="RentalCar" table="RentalCar_TransitStrategy">

        <key column="TransitStrategyId" />

        <property name="RentalCarBranch" />
        <property name="CarMake" />
        <property name="CarModel" />
        <property name="CarYear" />
        <property name="CarColor" />
        <property name="RentalBegins" />
        <property name="RentalEnds" />

    </joined-subclass>

</class>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               assembly="Vacationizer"
               namespace="Vacationizer.Domain.Transit">

<class name="TransportationCompany">

    <id name="TransportationCompanyId">
        <generator class="guid" />
    </id>

    <property name="Name" />
    <property name="Reviews" />
    <property name="Website" />
    <property name="Photo" />

    <joined-subclass name="Airline" table="Airline_TransportationCompany">

        <key column="TransportationLocationId" />

    </joined-subclass>

    <joined-subclass name="RentalCarAgency" table="RentalCarAgency_TransportationCompany">

        <key column="TransportationLocationId" />

    </joined-subclass>

</class>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
               assembly="Vacationizer"
               namespace="Vacationizer.Domain.Transit">

<class name="TransportationLocation">

    <id name="TransportationLocationId">
        <generator class="guid" />
    </id>

    <property name="Name" />
    <property name="Image" />
    <property name="Geolocation" />
    <property name="Reviews" />
    <!-- <property name="HoursOpen" />-->
    <property name="PhoneNumber" />
    <property name="FaxNumber" />

    <joined-subclass name="Airport" table="Airport_TransportationLocation">

        <key column="TransportationLocationId" />

        <property name="AirportCode" />
        <property name="Website" />

    </joined-subclass>

    <joined-subclass name="RentalCarBranch" table="RentalCarBranch_TransportationLocation">

        <key column="TransitStrategyId" />

        <property name="Agency" />

    </joined-subclass>

</class>

但是,每当我尝试使用此模式时,我都会收到此错误/堆栈跟踪:

------ Test started: Assembly: Vacationizer.Tests.dll ------

TestCase 'M:Vacationizer.Tests.VacationRepository_Fixture.TestFixtureSetUp'
failed: Could not compile the mapping document:      Vacationizer.Mappings.TransitStrategy.hbm.xml
    NHibernate.MappingException: Could not compile the mapping document: Vacationizer.Mappings.TransitStrategy.hbm.xml ---> NHibernate.MappingException: Problem trying to set property type by reflection ---> NHibernate.MappingException: class Vacationizer.Domain.Transit.RentalCar, Vacationizer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null not found while looking for property: RentalCarBranch ---> NHibernate.PropertyNotFoundException: Could not find a getter for property 'RentalCarBranch' in class 'Vacationizer.Domain.Transit.RentalCar'
    at NHibernate.Properties.BasicPropertyAccessor.GetGetter(Type type, String propertyName)
    at NHibernate.Util.ReflectHelper.ReflectedPropertyClass(String className, String name, String accessorName)
       --- End of inner exception stack trace ---
    at NHibernate.Util.ReflectHelper.ReflectedPropertyClass(String className, String name, String accessorName)
    at NHibernate.Mapping.SimpleValue.SetTypeUsingReflection(String className, String propertyName, String accesorName)
       --- End of inner exception stack trace ---
    at NHibernate.Mapping.SimpleValue.SetTypeUsingReflection(String className, String propertyName, String accesorName)
    at NHibernate.Cfg.XmlHbmBinding.ClassBinder.CreateProperty(IValue value, String propertyName, String className, XmlNode subnode, IDictionary`2 inheritedMetas)
    at NHibernate.Cfg.XmlHbmBinding.ClassBinder.PropertiesFromXML(XmlNode node, PersistentClass model, IDictionary`2 inheritedMetas, UniqueKey uniqueKey, Boolean mutable, Boolean nullable, Boolean naturalId)
    at NHibernate.Cfg.XmlHbmBinding.JoinedSubclassBinder.HandleJoinedSubclass(PersistentClass model, XmlNode subnode, IDictionary`2 inheritedMetas)
    at NHibernate.Cfg.XmlHbmBinding.ClassBinder.PropertiesFromXML(XmlNode node, PersistentClass model, IDictionary`2 inheritedMetas, UniqueKey uniqueKey, Boolean mutable, Boolean nullable, Boolean naturalId)
    at NHibernate.Cfg.XmlHbmBinding.RootClassBinder.Bind(XmlNode node, HbmClass classSchema, IDictionary`2 inheritedMetas)
    at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddRootClasses(XmlNode parentNode, IDictionary`2 inheritedMetas)
    at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind(XmlNode node)
    at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc)
       --- End of inner exception stack trace ---
    at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
    at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc)
    at NHibernate.Cfg.Configuration.ProcessMappingsQueue()
    at NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument document)
    at NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String name)
    at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name)
    at NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly)
    at NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly)
    at NHibernate.Cfg.Configuration.AddAssembly(String assemblyName)
    at NHibernate.Cfg.Configuration.DoConfigure(IHibernateConfiguration hc)
    at NHibernate.Cfg.Configuration.Configure()
    VacationRepository_Fixture.cs(24,0): at Vacationizer.Tests.VacationRepository_Fixture.TestFixtureSetUp()

0 passed, 1 failed, 0 skipped, took 8.38 seconds (Ad hoc).

关于如何以不同方式实现这一点的任何想法?非常感谢!

2 个答案:

答案 0 :(得分:2)

错误本身很明显:RentalCar的映射定义了RentalCarBranch属性,可能没有声明(检查拼写错误;我个人将其命名为属性Branch)。

还有另一个错误:当所有持久性实体引用都应声明为<property>时,您将所有内容定义为<many-to-one>

答案 1 :(得分:0)

从我在异常中看到的情况来看,它表示你的RentalCar类没有声明RentalCarBranch属性。

是这样的吗?