How to create a model and mapping if properties duplicate?

Hello guys,

this seems like a trivial problem but I want to handle it in the best possible way.

I have a scenario where a user has to submit a form with some user information:

a) Name
b) Surname
c) Date of birth
e) Registered adress
i. Street
ii. house number
iii.Zip code
iv. province
v. district
vi. city
f) Correspondence adress
i. Street
ii. house number
iii.Zip code
iv. province
v. district
vi. city

As you can see some properties duplicate for correspondence and registered adresses. Now I thought how to model this scenario in the database and it would look something like this:

Table registration:

 - ID
 - Name
 - SurName
 - DateOfBirth
 - ID_CorresponenceAdress
 - ID_RegisteredAdress

Correspondence and registered adresses have the same fields so basically I can create a second table with all the data needed for adresses:

Table adress:

 - ID
 - Street
 - house number
 -Zip code
 - province
 -district
 -city
 - Type

Also a type is specified to tell whether it’s correspondence or registered adress.

The problem is I’m not sure if below is the best Model for this scenario and how to make a mapping of this Model using NHibernate?

        public class Registration
    {
        public virtual int ID { get; set; }
        public virtual string Name { get; set; }
        public virtual string Surname { get; set; }
        public virtual Adress RegisteredAdress { get; set; }
        public virtual Adress CorrespondenceAdress { get; set; }
    }

    public class Adress
    {
        public virtual int ID { get; set; }
        public virtual string Street { get; set; }
        public virtual string HouseNumber { get; set; }
        public virtual int ZipCode { get; set; }
        public virtual string Province { get; set; }
        public virtual string District { get; set; }
        public virtual string City { get; set; }
        public virtual string Type { get; set; }
    }

Cahir

if below is the best Model for this scenario

Yes. It is best way to model your classes

Cahir

how to make a mapping of this Model using NHibernate

This is like a one to one mapping. Please take a look at this blog

http://thesenilecoder.blogspot.in/2012/11/nhibernate-one-to-one-mapping-with.html

Hello thanks for the reply.

Could you help me out in creating these mapping documents ?

I don’t seem to grasp how this should be done. This is what I’ve got so far and I don’t know what should go into many-to-one in my case

  <class name="Registration" table="Registrations">
    <id name="ID" unsaved-value="0">
      <column name="ID"></column>
      <generator class="native" />
    </id>

    <property name="Name"  />
    <property name="Surname"  />
    
    <one-to-one name="RegisteredAdress"
                foreign-key="none"
                constrained="true"
                class="Adress" />





  </class>

  <class  name="Adress" table="Addresses">
    <id name="ID">
      <column name="ID"></column>
      <generator class="native"/>
    </id>
    <property name="Street" />

    <many-to-one name="?" unique="true" column="?" />
    
  </class>

I think you don’t need mapping from Address. I typically generate the mappings from database (reverse engineering) and tweak them as needed. Have you tried this?

If I remove mapping from Adress I get LazyInitializationException. How do you generate the mappings ? Using some kind of mapping generator ?

EDIT:

Ok It’s working if I remove using from my repository.

        public IList<T> Select()
        {
            ISession session = NHibernateHelper.OpenSession(); //WORKING

            //using (ISession session = NHibernateHelper.OpenSession())  NOT WORKING

                return session.CreateCriteria<T>().List<T>();
        }

If I change it like this wont this cause any problems ?

Leave a Reply