NHibernate many-to-many with composite element

Posted by E1 on Stack Overflow See other posts from Stack Overflow or by E1
Published on 2010-04-04T15:01:46Z Indexed on 2010/04/04 15:13 UTC
Read the original article Hit count: 680

Filed under:
|
|

Hello everyone.

I encountered the following problem. In my application uses the entities Owner and Area binding as many to many.

public class Area : DomainObject<int> {        
    private ISet<OwnersPeriod> _owners = new HashedSet<OwnersPeriod>();

    public ICollection<OwnersPeriod> Owners {
        get { return _owners; }
        set {
            Check.Require(value != null);
            _owners = new HashedSet<OwnersPeriod>(value);
        }
    }
}

Table Owner2Area has the following fields:

CREATE TABLE [dbo].[Owner2Area](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [IDOwner] [int] NOT NULL,
    [IDArea] [int] NOT NULL,
    [FirstDate] [datetime] NOT NULL,
    [LastDate] [datetime] NULL,
 CONSTRAINT [PK_Owner2Area] PRIMARY KEY CLUSTERED)

Therefore corresponds to the class OwnersPeriod

    public class OwnersPeriod {
        private Owner _member;
        private Area _area;    
        public Owner Owner { get {...} set{...} }    
        public Area Area { get { ... } set { ... } }    
        public  DateTime FirstDate { get; set; }    
        public  DateTime? LastDate { get; set; }       
    }

I wrote mappings

<class lazy="false" name="Core.Domain.Area, Core" table="Areas">
   ...
   <set name="Owners" table="Owner2Area" inverse="true" lazy="true" >
      <key column="IDArea"/>
      <composite-element class="Core.Domain.OwnersPeriod, Core" >
        <parent name="Area" />
        <property name="FirstDate" type="datetime"/>
        <property name="LastDate" type="datetime"/>
        <many-to-one name="Owner" class="Core.Domain.Owner, Core" column="IDOwner"/>
      </composite-element>
    </set>
</class>

For each area existing data are successfully loaded into Owners, but when I add new record in Owner2Area through CreateSQLQuery, these data are not updated for instance of area. If I re-opened the form and got all areas, added link successfully loaded into the collection.
How can be forced to load added thus recording of relation many-to-many?
Nhibernate v.2.0.1, db MSSQL 2005

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#