Wednesday, November 16, 2011

NHibernate.MappingException: Could not determine type for: MyClass, for columns: NHibernate.Mapping.Column(id)

If you are using conformist mapping-by-code introduced by NHibernate in the 3.2, and are facing this exception maybe you have another problem.
If you define a Bag map like this
Bag(x => x.CollectionProp
     , map =>
     {
        map.Key(km => km.Column("col_id"));
        map.Cascade(Cascade.All | Cascade.DeleteOrphans);
     });
for a one-to-many association, you will retrieve this (misleading) exception. To solve it you have to specific the action in a way like this:
Bag(x => x.CollectionProp
     , map =>
     {
        map.Key(km => km.Column("col_id"));
        map.Cascade(Cascade.All | Cascade.DeleteOrphans);
     }
     , action => action.OneToMany()
    );
A post about it on stack overflow.

11 comments:

  1. That problem has just wasted a few hours of my life! Thank you for posting the solution.

    ReplyDelete
  2. Man, you saved my day!

    ReplyDelete
  3. You helped me out. Didn't know where to look. Much obliged.

    ReplyDelete
  4. Thank you sir !

    I was trying out mapping by code (mixed with hbm files)
    and this exception was driving me crazy.

    The funny thing is, it identified wrong column mapping for class mapped by hbm file.

    Adding the action solved the issue.
    btw.: also applies to ISet.

    ReplyDelete
  5. You're welcome!
    Good, another case to add to this exception .. =)

    ReplyDelete
  6. Dude, right now I would be prepared to have your babies (if it wasn't biologically impossible).

    Thank you! There's no way I would have looked there.

    ReplyDelete