Mapping by code - a bag

69 views
Skip to first unread message

Richard Wilde

unread,
Jun 10, 2011, 3:49:34 AM6/10/11
to nhusers
Hi

Still playing with nh 3.2 mapping by code and I am now stumbling
around trying to work out how to map a bag. I get the following
exception when making a call to BuildSessionFactory

Could not determine type for: Domain.Model.Entities.Actor,
Domain.Model, for columns: NHibernate.Mapping.Column(id)

This is what I am trying to achieve:-

<bag name="ActorList" cascade="all-delete-orphan">
<key column="MovieId" />
<one-to-many class="Actor"/>
</bag>

my Movie Mapping Class

public class MovieMapping : SubclassMapping<Movie>
{
public MovieMapping()
{
DiscriminatorValue("Movie");
Property(x => x.Director, x => x.NotNullable(true));

Bag(x => x.ActorList, bag =>
{
bag.Key(k => k.ForeignKey("MovieId"));
bag.Cascade(Cascade.All | Cascade.DeleteOrphans);
});
}
}

If I remark out the Bag then BuildSessionFactory is fine


My Actor Mapping Class

public class ActorMapping : ClassMapping<Actor>
{
public ActorMapping()
{
Id(x => x.Id, map => map.Generator(Generators.GuidComb));
Table("ActorRole");
Property(x => x.Name, x => x.NotNullable(true));
Property(x => x.Role, x => x.NotNullable(true));
}
}

My Actor Class

namespace Domain.Model.Entities
{
public class Actor : Entity
{
public virtual string Name { get; set; }
public virtual string Role { get; set; }
}
}

My base Entity Class

public abstract class Entity
{
public virtual Guid Id { get; protected set; }
}

Richard Wilde

unread,
Jun 10, 2011, 4:14:16 AM6/10/11
to nhusers
To simplify this I have found the following:-

        Bag(x => x.ActorList, bag =>
            {
                bag.Key(k => k.Column(col=>col.Name("MovieId")));
                bag.Cascade(Cascade.All | Cascade.DeleteOrphans);
            });

creates

<bag name="ActorList" cascade="all,delete-orphan">
      <key column="MovieId" />
      <element type="Domain.Model.Entities.Actor, Domain.Model" />
    </bag>

whereas I want this

<bag name="ActorList" cascade="all-delete-orphan">
   <key column="MovieId"  />
   <one-to-many class="Actor"/>
</bag>

So I am missing a piece of the jigsaw...

Kenneth

unread,
Jun 10, 2011, 4:38:00 AM6/10/11
to nhusers
There is a third actionmethod on the Bag method, where you can set it.
Something like:

Bag(x => x.ActorList, bag =>
{
bag.Key(k => k.Column(col=>col.Name("MovieId")));
bag.Cascade(Cascade.All | Cascade.DeleteOrphans);
},
rel => rel.OneToMany()
);

Kenneth

Richard Wilde

unread,
Jun 10, 2011, 4:36:30 AM6/10/11
to nhusers
Sorry for wasting your time but I have found a solution

Bag(x => x.ActorList, bag =>
{

bag.Key(k => k.Column(col => col.Name("MovieId")));
bag.Cascade(Cascade.All | Cascade.DeleteOrphans);
},
action => action.OneToMany());

I hope that it will one day help somebody else. I suppose I need to
look up conventions as it appears this may be the place to help reduce
code,
Message has been deleted

Richard Wilde

unread,
Jun 10, 2011, 4:54:46 AM6/10/11
to nhu...@googlegroups.com
Thanks


--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.


Fabio Maulo

unread,
Jun 11, 2011, 2:06:26 PM6/11/11
to nhu...@googlegroups.com
If you need convention-based-mapping you can:

1) use ModelMapper injecting SimpleModelInspector
Example:
var inspector = new SimpleModelInspector();
inspector.IsEntity((type, declared) => type == typeof(Inherited));
inspector.IsRootEntity((type, declared) => type == typeof(Inherited));
var mapper = new ModelMapper(inspector);

2) use ConventionModelMapper instead ModelMapper
3) use ModelMapper injecting your own impl of IModelInspector
4) use ModelMapper with its events
5) ConfORM yourself LOL!!
Reply all
Reply to author
Forward
0 new messages