tags:

views:

176

answers:

2

I have been struggling with this NHibernate issue for hours. I extensively researched on the web as well as the NHibernate documentation, and I can make no sense of this issue. I'm relatively new to NHibernate, and love it. In that case, though, it's driving me mad.

I'm writing a small "Poll" module for a website. I have several classes (Poll, PollVote and PollAnswer). The main one, Poll, is the one causing the issue. This this what the class looks like:

public class Poll
    {
        public virtual int Id { get; set; }
        public virtual Site Site { get; set; }
        public virtual string Question { get; set; }
        public virtual bool Locked { get; set; }
        public virtual bool Enabled { get; set; }
        public virtual ICollection<PollAnswer> AnswersSet { get; set; }
        public virtual ICollection<PollVote> VotesSet { get; set; }
    }

And this is what the mapping looks like:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="Folke"
                   namespace="Folke.Code.Domain">
  <class name="Poll">
    <id name="Id">
      <generator class="native"></generator>
    </id>
    <property name="Site"/>
    <property name="Question"/>
    <property name="Locked"/>
    <property name="Enabled"/>
    <set name="AnswersSet" lazy="true">
      <key column="PollId"/>
      <one-to-many class="PollAnswer"/>
    </set>
    <set name="VotesSet" lazy="true">
      <key column="PollId"/>
      <one-to-many class="PollVote"/>
    </set>
  </class>
</hibernate-mapping>

This returns me an error:

Association references unmapped class: Folke.Code.Domain.PollAnswer Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: NHibernate.MappingException: Association references unmapped class: Folke.Code.Domain.PollAnswer

I really want to understand this, and understand better the inner working of NHibernate. I don't understand how it cannot "see" the PollAnswer" class.

+1  A: 

You're defining a one to many relationship between Poll and PollAnswer. Therefore, you'll need a section for PollAnswer in your hbm.xml file. You'll need one for PollVote as well.

anthony
A: 

And if I get the same problem using Fluent Nhibernate and wants to write it in code?

My error was relating "System.Xml.XmlAttribute".

Edit, I think I solved it:

 var cfg = new NotifyFluentNhibernateConfiguration();

            return Fluently.Configure()
              .Database(
               FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2005
                    .ConnectionString("[MYSERVERADRESS];Database=NotifyTest;User ID=NHibernateTester;Password=[MYPASSWORD];Trusted_Connection=False;")
              )

              .Mappings(m => {
                  m.AutoMappings
                    .Add(AutoMap.AssemblyOf<SubscriptionManagerRP>(cfg));

              } )

              .BuildSessionFactory();




 class MyFluentNhibernateConfiguration : DefaultAutomappingConfiguration
    {
        public override bool ShouldMap(Type type)
        {
            return type.Namespace == "System.Xml.XmlAttribute";
        }


    }
Simply G.