views:

46

answers:

2

I have 2 entities- Classroom and Section, that I need help with NHibernate mapping. A Classroom has a collection of Sections. And the Section has a reference back to its owner Classroom.

On the code side:

public class Classroom
{
    public int Id { get; set; }         
    public ISet<Section> Sections { get; set; } 
}

public class Section
{
    public int Id { get; set; } 
    public Classroom Classroom { get; set; } 
}

On the database side:

CREATE TABLE Classroom (
   ClassroomID int
)

CREATE TABLE ClassroomSection (
   ClassroomID int, 
   SectionID int,
   IsActive bit
)

CREATE TABLE Section (
   SectionID
)

As seen above, even though this is a one-to-many mapping, there is a 3rd mapping table ClassroomSection. Moveover this mapping table has some of its own fields, like IsActive. I don't want to create an entity for ClassroomSection in my code because it doesn't have any domain logic. But I do want to have access to the fields in this table. Any help with bidirectional mapping is appreciated.

Thanks!

A: 

It sounds like ClassroomSection is a value object rather than an entity, is that correct?

Chance
I don't think ClassroomSection is anything. Its just a mapping table between Classroom and Section.
Rohit Agarwal
A: 

Post some sample code of how you'd like to be able to access IsActive.

Ayende's latest post might give some hints to what you're trying to accomplish: http://ayende.com/Blog/archive/2009/06/10/nhibernate-ndash-query-only-properties.aspx

Ben
You ask an excellent question. I don't know why I didn't think about this before. How should I access IsActive? I could have it live in the Section entity. Then I could do a <join> to ClassroomSection in the Section mapping. But Section itself has an IsActive field. Looks like I may have to create a ClassroomSection entity after all. I don't think there is getting around it.
Rohit Agarwal