1

just started on a project to convert a NHibernate Fluent mapping into the NHibernate Mapping By Code as part of an upgrade associated with one of my old applications.

Almost there, but I stumbled upon something I can't convert properly and found myself stumped. Now I hope maybe some of you experienced out there can help me with it. Below is the original mapping using Fluent:

HasMany<ExampleEntity>(x => x.OtherExampleEntities)
            .OptimisticLock.False()
            .AsSet()
            .KeyColumn("ParentExampleEntityId")
            .Inverse()
            .Cascade.SaveUpdate();

Now I got stuck on converting the KeyColumn-part of my mapping, below is my current progress (thus keyColumn still being there):

Set(x => x.OtherExampleEntities, x => {
            x.OptimisticLock(false);
            x.KeyColumn("ParentExampleEntityId");
            x.Inverse(true);
            x.Cascade(Cascade.Persist);
        }, map => map.OneToMany(r => r.Class(typeof(ExampleEntity))));

There's not a lot of documentation regarding the mapping by code part of NHibernate but I've been spending a lot of time with the posts made by notherdev (@blogspot.se). All help is appreciated.

1 Answers1

0

There is a comprehensive post about <set> mapping

Mapping-by-Code - Set and Bag by Adam Bar

Small snippet, but please observe the post:

Set(x => x.Users, c =>
{
    c.Fetch(CollectionFetchMode.Join); // or CollectionFetchMode.Select,
                                       //    CollectionFetchMode.Subselect
    c.BatchSize(100);
    c.Lazy(CollectionLazy.Lazy); // or CollectionLazy.NoLazy, CollectionLazy.Extra

    c.Table("tableName");
    c.Schema("schemaName");
    c.Catalog("catalogName");

    c.Cascade(Cascade.All);
    c.Inverse(true);

    c.Where("SQL command");
    c.Filter("filterName", f => f.Condition("condition"));
    c.OrderBy(x => x.Name); // or SQL expression

    c.Access(Accessor.Field);
    c.Sort<CustomComparer>();
    c.Type<CustomType>();
    c.Persister<CustomPersister>();
    c.OptimisticLock(true);
    c.Mutable(true);

<key column="" ...> mapping:

    c.Key(k =>
    {
        k.Column("columnName");
        // or...
        k.Column(x =>
        {
            x.Name("columnName");
            // etc.
        });

        k.ForeignKey("collection_fk");
        k.NotNullable(true);
        k.OnDelete(OnDeleteAction.NoAction); // or OnDeleteAction.Cascade
        k.PropertyRef(x => x.Name);
        k.Unique(true);
        k.Update(true);
    });
    ....
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • Adam Bar's blog posts are the best documentation you are going to find about all aspects of mapping by code. Here's a link with a OneToMany mapping http://stackoverflow.com/questions/7339463/nhibernate-conformist-mapping-unable-to-determine-type/7363653#7363653 – Fran Oct 14 '14 at 13:12
  • Yeah, thanks, I just noticed it too! Seems to be working, one step closer to glory eh.. Thanks! – Monsterlokomotivet Oct 14 '14 at 13:16
  • Great if that helped anyhow. NHibernate is amazing tool. Nice to see you want to use the mapping by code! Great ;) – Radim Köhler Oct 14 '14 at 13:17