系列抽象类和NHibernate

Posted

技术标签:

【中文标题】系列抽象类和NHibernate【英文标题】:Series of abstract classes and NHibernate 【发布时间】:2010-05-08 20:57:24 【问题描述】:

首先感谢您抽出宝贵时间查看此内容。

对于我正在进行的一个研究项目,我有一个有点复杂的设计(我已经得到了)通过 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).

关于如何以不同方式实现此功能的任何想法?非常感谢!

【问题讨论】:

【参考方案1】:

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

还有另一个错误:您将所有内容定义为&lt;property&gt;,而所有持久实体引用都应声明为&lt;many-to-one&gt;

【讨论】:

迭戈,非常感谢您的意见,但这是我在复制问题时的错误。您绝对正确,因为您的解决方案可以解决它,但我相信另一个问题正在发挥作用。截至早上 7 点,我才开始工作,但还有很多复杂的事情阻碍了我。如果它通过了我的单元测试套件,我将在稍后发布我的修复。再次感谢您的帮助!【参考方案2】:

根据我在异常中看到的情况,这表明您的 RentalCar 类没有声明 RentalCarBranch 属性。

是这样吗?

【讨论】:

尼尔,请参阅我对 Diego 的上述评论。非常感谢!

以上是关于系列抽象类和NHibernate的主要内容,如果未能解决你的问题,请参考以下文章

面向对象 抽象类和接口

Java复习笔记1--抽象类和接口

抽象类和接口的区别

java中抽象类和接口的区别?

抽象类和接口的区别

抽象类和模板模式