Reputation: 11
I'm making a NHibernate demo for school, but I've a problem that I can't solve. You have a ticket and ticketresponses. A hardwareticket inherits from ticket. When the ReadTicket method is executed I get a WrongclassException saying that
"Object was not of the specified subclass: SC.BL.Domain.Ticket (Discriminator was:")"
If somebody can help me, I'll be very delighted.
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; }
}
Repository:
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;
}
}
Mapping:
<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>
Upvotes: 1
Views: 786
Reputation: 2439
Ticket is your main base class. You cannot add the same as subclass in your mapping.
<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>
Upvotes: 1