Nhibernate映射鉴别器“错误的类异常”

本文关键字:异常 错误 映射 鉴别 Nhibernate | 更新日期: 2023-09-27 18:34:02

我正在为学校制作一个NHibernate演示,但我有一个无法解决的问题。您有票证和票证响应。硬件票证继承自票证。当执行 ReadTicket 方法时,我得到一个 WrongclassException,说

"对象不属于指定的子类:SC。BL.域名票证 (鉴别器是:")"

如果有人能帮助我,我会很高兴。

public class Ticket
  {
    public virtual int AccountId { get; set; }
    public virtual DateTime DateOpened { get; set; }
    [Required]
    [MaxLength(100, ErrorMessage = "Er zijn maximaal 100 tekens toegestaan")]
    public virtual string Text { get; set; }
    public virtual int TicketNumber { get; set; }
    public virtual TicketState State { get; set; }
    public virtual IList<TicketResponse> Responses { get; set; }
  }
public class HardwareTicket : Ticket
  {
    [RegularExpression("^(PC-)[0-9]+")]
    public virtual string DeviceName { get; set; }
  }

存储 库:

 public class TicketRepository : ITicketRepository
      {    private static ISessionFactory mySessionFactory;
       Private static ISession mySession;
        public TicketRepository()
        {
          if (mySessionFactory == null)
          {
            var configuration = new Configuration().Configure("hibernate.cfg.xml");
            configuration.AddAssembly(typeof(Ticket).Assembly);
            configuration.AddAssembly(typeof(TicketResponse).Assembly);
            mySessionFactory = configuration.BuildSessionFactory();
          }
        }
public Ticket ReadTicket(int ticketNumber)
    {
      using (mySession = mySessionFactory.OpenSession())
      {
        var ticket = mySession.QueryOver<Ticket>().Where(x => x.TicketNumber == ticketNumber).SingleOrDefault();
        return ticket;
      }
    }

映射:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                    assembly="SC.BL.Domain"
                 namespace ="SC.BL.Domain">

  <class name="Ticket" table="Ticket" lazy ="true">
    <id name="TicketNumber">
      <generator class="identity"> </generator>
    </id>
   <discriminator column="Discriminator" type="System.String"  not-null="true"/>
    <property name="AccountId"></property>
    <property name="Text"></property>
    <property name="DateOpened"></property>
    <property name="State"></property>

    <bag name="Responses" lazy="true" inverse="true"
                          batch-size="25" cascade="all-delete-orphan">
      <key column="TicketNumber" />
      <one-to-many class="TicketResponse" />
    </bag>
    <subclass name="HardwareTicket" discriminator-value ="HardwareTicket" >
      <property name="DeviceName" not-null="false"></property>
    </subclass>
    <subclass name="Ticket" discriminator-value ="Ticket" >
    </subclass>
  </class>
</hibernate-mapping>

Nhibernate映射鉴别器“错误的类异常”

Ticket 是你的主要基类。不能在映射中添加与子类相同的子类。

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                    assembly="SC.BL.Domain"
                 namespace ="SC.BL.Domain">

  <class name="Ticket" table="Ticket" lazy ="true">
    <id name="TicketNumber">
      <generator class="identity"> </generator>
    </id>
   <discriminator column="Discriminator" type="System.String"  not-null="true"/>
    <property name="AccountId"></property>
    <property name="Text"></property>
    <property name="DateOpened"></property>
    <property name="State"></property>

    <bag name="Responses" lazy="true" inverse="true"
                          batch-size="25" cascade="all-delete-orphan">
      <key column="TicketNumber" />
      <one-to-many class="TicketResponse" />
    </bag>
    <subclass name="HardwareTicket" discriminator-value ="HardwareTicket" >
      <property name="DeviceName" not-null="false"></property>
    </subclass>
  </class>
</hibernate-mapping>