NHibernate - 继承映射问题与每个子类策略的表

时间:2011-03-02 08:34:19

标签: vb.net nhibernate nhibernate-mapping

我们将每个具体类的表映射更改为子类策略,现在UnitTests失败了,但我无法识别原因。

有一个基类文章和一个半成品没有特定属性的孩子。

Article.hbm.xml:

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"

   <class name="Article" table="cvm_anagrafica_articoli" lazy="true" dynamic-insert="true" dynamic-update="true">



      <id name="Id" column="id" type="integer">
         <generator class="native" >
            <param name="sequence">seq_anagrafica_articoli</param>
         </generator>
      </id>

      <version name="Version" column="versione" unsaved-value="0"/>

      <property name="Code" column="codice" />
      <property name="Description" column="descrizione" />

      <discriminator column="ArticleType" type="string"/>

      <subclass name="SemifinishedArticle" discriminator-value="S" />
   </class>
</hibernate-mapping>

SemifinishedArticle.hbm.xml:

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"

    <subclass name="SemifinishedArticle" extends="Article" lazy="true" >


    </subclass>
</hibernate-mapping>

SemifinishedArticle.cs

Public Class SemifinishedArticle
    Inherits Article

End Class

错误消息:

Initialization method CVM050__Recipe_Management_Test.ArticleMappingTest.MyTestInitialize threw exception. NHibernate.MappingException: NHibernate.MappingException: Coveme.Core.Article.hbm.xml(52,4): XML validation error: The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'discriminator' in namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'property, many-to-one, one-to-one, component, dynamic-component, properties, any, map, set, list, bag, idbag, array, primitive-array, join, subclass, joined-subclass, union-subclass, loader, sql-insert, sql-update, sql-delete, filter, resultset, query, sql-query' in namespace 'urn:nhibernate-mapping-2.2'. ---> System.Xml.Schema.XmlSchemaValidationException: The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'discriminator' in namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'property, many-to-one, one-to-one, component, dynamic-component, properties, any, map, set, list, bag, idbag, array, primitive-array, join, subclass, joined-subclass, union-subclass, loader, sql-insert, sql-update, sql-delete, filter, resultset, query, sql-query' in namespace 'urn:nhibernate-mapping-2.2'..

错误堆栈跟踪:

NHibernate.Cfg.Configuration.LogAndThrow(Exception exception) in d:\CSharp\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs: line 340
NHibernate.Cfg.Configuration.ValidationHandler(Object o, ValidationEventArgs args) in d:\CSharp\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs: line 1838
System.Xml.Schema.XmlSchemaValidator.SendValidationEvent(ValidationEventHandler eventHandler, Object sender, XmlSchemaValidationException e, XmlSeverityType severity)
System.Xml.Schema.XmlSchemaValidator.ElementValidationError(XmlQualifiedName name, ValidationState context, ValidationEventHandler eventHandler, Object sender, String sourceUri, Int32 lineNo, Int32 linePos, XmlSchemaSet schemaSet)
System.Xml.Schema.XmlSchemaValidator.ValidateElementContext(XmlQualifiedName elementName, Boolean& invalidElementInContext)
System.Xml.Schema.XmlSchemaValidator.ValidateElement(String localName, String namespaceUri, XmlSchemaInfo schemaInfo, String xsiType, String xsiNil, String xsiSchemaLocation, String xsiNoNamespaceSchemaLocation)
System.Xml.XsdValidatingReader.ProcessElementEvent()
System.Xml.XsdValidatingReader.ProcessReaderEvent()
System.Xml.XsdValidatingReader.Read()
System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)
System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
System.Xml.XmlDocument.Load(XmlReader reader)
NHibernate.Cfg.Configuration.LoadMappingDocument(XmlReader hbmReader, String name) in d:\CSharp\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs: line 1776
NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String name) in d:\CSharp\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs: line 1813
NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name) in d:\CSharp\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs: line 628
NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly) in d:\CSharp\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs: line 666
NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly) in d:\CSharp\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs: line 761
CVM050__Recipe_Management_Test.DbTestsBase`1.Init(Assembly[] assembliesWithMappings) in C:\UsersData\jcocchi\CVM050 - Recipe Management\Trunk\src\CVM050 -Recipe Management Test\DBTestsBase.cs: line 44
CVM050__Recipe_Management_Test.ArticleMappingTest.MyTestInitialize() in C:\UsersData\jcocchi\CVM050 - Recipe Management\Trunk\src\CVM050 -Recipe Management Test\ArticleMappingTest.cs: line 58

谢谢你们

2 个答案:

答案 0 :(得分:3)

  1. SemifinishedArticle.hbm.xml是多余的(已经与文章一起定义)
  2. <discriminator>介于<id><version>之间。订单在NH模式中很重要。
  3. 无论如何,你有一个SemifinishedArticle课程让你自己陷入困境: 它完成后你打算做什么?删除它并插入文章?请记住,您无法更改对象的类。

    最好添加ArticleState属性。

答案 1 :(得分:1)

article.hbm.xml你为什么有?

<subclass name="SemifinishedArticle" discriminator-value="S" />

这应该在SemifinishedArticle.hbm.xml