0

I have a table schema like this

Id CompanyName EmailAddress EmailAddressHash Phone1 Phone1Hash Phone2 Phone2Hash 

Class members like this

 class HashValue { //properties public string OriginalValue; public string HashValue; } class HashedApplication { //properties public HashValue Email; /*Phone 1 and 2*/ public List<HashValue> PhoneNumbers; public int Id; Public string CompanyName; } 

I am wondering how to map this table and classes in NHibernate.

8
  • @Daniel no I did not try Fluent NHibernate. Can we map this without it? Commented Feb 24, 2011 at 16:41
  • You could.. but why would you want to? Commented Feb 24, 2011 at 16:42
  • Your properties should be virtual for starters. Is it possible for your to refactor the database design. You have a parent child relationship Company and Details? Commented Feb 24, 2011 at 16:46
  • @ShaneC There is additional learning curve to implement Fluent NHibernate. That is it. Commented Feb 24, 2011 at 16:51
  • @Jonathan I could refactored my database design and normalize. For the sake of NHibernate should I change my data model? Commented Feb 24, 2011 at 16:53

2 Answers 2

2

You should map the Email property of the HashedApplication class as a component. Therefore, it would also be better if you create the HashValue type as a value type.

The PhoneNumbers collection can be mapped as a set, which contains a composite-element:

Offcourse, this means that you'll have to change your data-model for the PhoneNumbers collection; you'll have to create another table which contains the PhoneNumbers that are related to a specific HashedApplciation. (Which is offcourse better, since your datamodel is then normalized).

Sign up to request clarification or add additional context in comments.

2 Comments

you mean to I must normalize the table in order to get that work with NHibernate?
I am going to use component for Email Address. And refactor HashesApplication class. It would remove list of PhoneNumbers property and add two more HashValue property PhoneNumber1 and PhoneNumber2. I know this is dirty. +1 for you answer.
1

I would not waste my time trying to figure out how to bend NHibernate to work with this poorly designed table structure, out of fear (I might encounter this solution in the future), I provide the following ;-) If you can change it, then normalize the database correctly.

I would start by refactoring the design. Create

Company Table[Id, CompanyName EmailAddress EmailAddressHash] 

and

ContactDetails[Id, CustomerId, Phone, PhoneHash ] 

Create a Many-to-one relationship. Then look at creating NHibernate mapping.

Create the following Mappings.

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="STOE.Bll" assembly="Bll"> <class name="Company" table="CompanyTable" mutable="false"> <id name="Id" column="Id" unsaved-value="0"> <generator class="identity"/> </id> <property name="Name" column="CompanyName" /> <property name="EmailAddress" column="EmailAddress" /> <property name="EmailAddressHash" column="EmailAddressHash" /> <bag name="CompanyContactDetails" lazy="false" table="ContactDetails" inverse="true" cascade="all"> <key column="CustomerId" /> <one-to-many class="STOE.Bll.CompanyContactDetail, Bll" /> </bag> </class> </hibernate-mapping> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="STOE.Bll" assembly="Bll"> <class name="CompanyContactDetail" table="CompanyContactDetail" mutable="false"> <id name="Id" column="Id" unsaved-value="0"> <generator class="identity"/> </id> <property name="Phone" column="Phone" /> <property name="PhoneHash" column="PhoneHash" /> </class> </hibernate-mapping> 

4 Comments

this is better way of implementing the model. I would go for Component mapping for HashValue property that is Email Address.
The properties do not have to be virtual, if you specify that NHibernate should not use dynamic proxies.
@Frederick Gheysels Cool, didn't know that, thanks for commenting!!
@Amzath Yeah by all means use a component mapping, I use them but sometimes they irritate me because the object can be null which can mean an extra check. So when there is no email address and you render the value without checking for null ... boom ... where as a standard string would print nothing. Just something to consider.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.