views:

388

answers:

2

Here is a simplified version of my database model. I have two tables: "Image", and "HostingProvider" which look like this:

[Image]

  • id
  • filename
  • hostingprovider_id

[HostingProvider]

  • id
  • base_url

Image HostingproviderId is a many-to-one foreign key relationship to the HostingProvider table. (Each image has one hosting provider).

Essentially I want to be able to have my Image class look like this:

[Image]

  • Id
  • base_url
  • filename

In NHibernate, how can I create a mapping file that will combine the base_url from the HostingProvider table, into the Image class?

A: 
public class Image {
    public virtual HostingProvider HostingProvider { get; set; } // NHibernate takes care of this
    public virtual string BaseUrl { get { return HostingProvider.BaseUrl; } }
}
Justice
That's not what the OP was asking. He wants one class, 2 tables.
Ben Scheirman
+2  A: 

What you're looking for is this:

http://ayende.com/Blog/archive/2007/04/24/Multi-Table-Entities-in-NHibernate.aspx

Here's a peek of what it looks like:

<class name="Person">

       <id name="Id" column="person_id" unsaved-value="0">

              <generator class="native"/>

       </id>



       <property name="Name"/>

       <property name="Sex"/>



       <join table="address">

              <key column="address_id"/>

              <property name="Address"/>

              <property name="Zip"/>

              <property name="Country"/>

              <property name="HomePhone"/>

              <property name="BusinessPhone"/>

       </join>

</class>
Ben Scheirman
That is exactly what I was looking for. Thanks so much !
David P
..then please mark it as "answered" :) Thanks!
Ben Scheirman