Search code examples
fluent-nhibernatenhibernate-mappingcomposite

Map Composition in Fluent NHibernate


There are 2 classes: Product and Image.

  1. The Product has only one Image.
  2. No orphan Images can exist.

This represent composition relationship in UML which means:
Assigning Product.Image a newImage results in following

  1. delete old image;
  2. insert new image;
  3. link new image to the product.

Now I need to map it to the RDBMS tables (meta-code):

Product (Id primary key, ImageId int references Image(id))  
Image(Id primary key, Content)

The question is HOW to do it using Fluent NHibernate.
PLEASE NOTE:

productMap.References(x => x.Image).Cascade.All()  

is not applicable - it DOES NOT delete the orphan image.

Also NH DOES NOT support all-delete-orphan for many-to-one, on-to-one.

I probably need something like join with component...
BUT IN FLUENT NH.

UPDATE: James in FNH user groups suggested this syntax:

WithTable("other table", m => 
{
  m.Component(...);
});

But no luck with it: NotSupportedException: Obsolete
It supposed to work in v1 (upcomming) of FNH.


Solution

  • You can map like a component ?

    Mapping with Fluent : http://wiki.fluentnhibernate.org/show/StandardMappingComponents

    NH doc : http://nhibernate.info/doc/nh/en/index.html#mapping-declaration-component