tags:

views:

89

answers:

1

how do i do this simple join either with hbm or fluently so i have a class like this?

public class Pet
{
    public Pet() {}

    public virtual int PetId { get; set; }
    public virtual int PetTypeId { get; set; }
    public virtual string Name { get; set; }
}

CREATE TABLE [dbo].[test_PetTypes](
    [PetTypeId] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nchar](10) NOT NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[test_Pet](
    [PetId] [int] IDENTITY(1,1) NOT NULL,
    [PetType] [int] NOT NULL
) ON [PRIMARY]

SELECT     dbo.test_Pet.*, dbo.test_PetTypes.Name
FROM         dbo.test_Pet INNER JOIN
                      dbo.test_PetTypes ON dbo.test_Pet.PetType = dbo.test_PetTypes.PetTypeId
A: 

This situation calls for a set with one-to-many mapping from test_PetType to test_Pet. You shouldn't need the ID fields on your class unless you particularly want to manipulate them by hand (NHibernate should handle it for you). There's a great post from Ayende (one of the NHibernate contributors) here.

I think the basic gist of your HBM (adapted from Ayende's post) should be similar to this:

<set name="Pets" table="test_PetTypes">
    <key column="PetId"/>
    <one-to-many class="Pet"/>
</set>

I don't have the facilities to test this out at the moment, so I can't verify the scenario. You might be able to get away without the <set /> tag and instead add the mapping at a higher level, for example.

alastairs