2
votes

Reverse engineering an existing database to map with N-Hibernate using Fluent N-Hibernate.

How can I map this?

Address table

Id
Address1
Address2

Person table

Id
First
Last

Types

Id
TypeName

PersonAddress table (A person can have home, business etc addresses)

Id

PersonId (Id from person table)

AddressId (Id from address table)

TypeId (Id from types lookup table HOME, BUSINESS etc..)

Any help would be great. Thanks

Here's another tricky one in addition to above mapping. Don't know how easy it would be to map it.

Party Table

Id Person Id points to Person

Identifiers Tables

Id Party Id Type Id Identifier value

Employee table

Employee Id No party or person table has foreign key to this table. The employee id is stored in the identifiers table. so for e.g. The identifier table is used store values for different types. The identifiers for a given party could DriverLicense, EmployeeId, SSN, Credit Card numeber etc, this could be many values.

Sample identifier data

Id, PartyId, TypeId, IdentifierValue

1 , 1, 1, EMPLID-1234 2 , 2, 1, EMPLID-4567 3 , 3, 1, EMPLID-34354

I am trying to get my head around this and just can't get it to mapped.

1

1 Answers

4
votes
// this answer assumes you have functional Address, Person, Type, and PersonAddress objects.

public class AddressMap : ClassMap<Address>
{
  public AddressMap()
  {
    Id(x=>x.Id);
    Map(x=>x.Address1);
    Map(x=>x.Address2);
  }
}

public class PersonMap : ClassMap<Person>
{
   public PersonMap()
   {
     Id(x=>x.Id);
     Map(x=>x.First);
     Map(x=>x.Last);
   }
}

public class TypeMap : ClassMap<Type>
{
   public TypeMap()
   {
     Id(x=>x.Id);
     Map(x=>x.TypeName);
   }
}

public class PersonAddressMap : ClassMap<PersonAddress>
{
   public PersonAddressMap()
   {
     Id(x=>x.Id);
     References(x=>x.Person, "PersonId");
     References(x=>x.Address, "AddressId");
     References(x=>x.Type, "TypeId");
   }
}