0

I am new in NHibernate and need help. I have two classes:

class Pop3
{
    public virtual long Id { set; get; }
    public virtual string HostName { set; get; }
    public virtual int Port { set; get; }
    public virtual bool UseSsl { set; get; }
}

class Email
{
    public virtual long Id { set; get; }
    public virtual string UserName { set; get; }
    public virtual string Password { set; get; }
    public virtual Pop3 Host { set; get; }
}

I need to map them to NHibernate (uses Sqlite). That is easy with Pop3 class

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                  assembly="TestAsm"
                  namespace="TestAsm.Entity.Mail">

  <class name="Pop3" table="pop3hosts">
    <id name="Id">
      <generator class="identity" />
    </id>
    <property name="HostName" />
    <property name="Port" />
    <property name="UseSsl" />
  </class>

</hibernate-mapping>

But how can I map Email class contains Pop3 class as property? My be i need to set Pop3.Id in Host property? But I think it is wrong way.

Alexey Kulikov
  • 1,097
  • 1
  • 14
  • 38

1 Answers1

2

This mapping belongs to the most basic, typical and well documented, I would say

An example, where the Pop3 class is mapped with many-to-one over the column Pop3Id:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                  assembly="TestAsm"
                  namespace="TestAsm.Entity.Mail">

  <class name="Email" table="email_table">
    <id name="Id" generator="identity" />

    <property name="UserName" />
    <property name="Password" />

    <many-to-one name="Host" column="Pop3Id" class="Pop3 " />

  </class>

</hibernate-mapping>

Please check the Chapter 21. Example: Parent/Child

Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • Do I need to set namespace in class attribute if class is in different name space like `class="TestAsm.Net.Pop3"`? – Alexey Kulikov Mar 12 '14 at 11:42
  • Great if that helped ;) `namespace` attribute on the class element is an abbrev, so if all referenced entities (many-to-one) are in the same ns ... no need to declare it again. but if class is in different, we have to provide its name with namespace. Enjoy NHibernate – Radim Köhler Mar 12 '14 at 12:07