views:

1178

answers:

3

Hi, I just fell in love with NHibernate and the fluent interface. The latter enables very nice mappings with refactoring support (no more need for xml files).

But nobody is perfect, so I am missing the many-to-any mapping in fluent. Does anybody know if it is already there? If so, a simple line of code would be nice.

But to stick to the header of the question, is there any way to combine fluent and normal NHibernate mapping.

Currently I use the following lines for my test setup WITH fluent, and the second code block for my test WITHOUT fluent (with XML mappings). How can I tell fluent to use fluent IF AVAILABLE and XML otherwise...

        var cfg = new Configuration();
        cfg.AddProperties(MsSqlConfiguration.MsSql2005.ConnectionString.Is(_testConnectionstring).ToProperties());
        cfg.AddMappingsFromAssembly(typeof(CatMap).Assembly);
        new SchemaExport(cfg).Create(true, true);

        var persistenceModel = new PersistenceModel();
        persistenceModel.addMappingsFromAssembly(typeof(CatMap).Assembly);
        IDictionary<string, string> properties = MsSqlConfiguration.MsSql2005.UseOuterJoin().ShowSql().ConnectionString.Is(_testConnectionstring).ToProperties();
        properties.Add("command_timeout", "340");

        session = new SessionSource(properties, persistenceModel).CreateSession();

Without Fluent...

        config = new Configuration();
        IDictionary props = new Hashtable();

        props["connection.provider"] = "NHibernate.Connection.DriverConnectionProvider";
        props["dialect"] = "NHibernate.Dialect.MsSql2005Dialect";
        props["connection.driver_class"] = "NHibernate.Driver.SqlClientDriver";
        props["connection.connection_string"] = "Server=localhost;initial catalog=Debug;Integrated Security=SSPI";
        props["show_sql"] = "true";
        foreach (DictionaryEntry de in props)
        {
            config.SetProperty(de.Key.ToString(), de.Value.ToString());
        }
        config.AddAssembly(typeof(CatMap).Assembly);

        SchemaExport se = new SchemaExport(config);
        se.Create(true, true);

        factory = config.BuildSessionFactory();
        session = factory.OpenSession();

That's it... Chris

PS: I really like this site, the GUI is perfect, and the quality of all articles is incredible. I think it will be huge :-) Have to register...

+2  A: 

Mapping from Foo to Baa:

HasManyToMany< Baa > ( x => Baas )
  .AsBag ( ) //can also be .AsSet()
  .WithTableName ( "foobar" )
  .WithParentKeyColumn ( "fooId" )
  .WithChildKeyColumn ( "barId" ) ;

Check out the examples in ClassMapXmlCreationTester - they also show what the default column names are.

David Kemp
+2  A: 

ManyToAny's currently aren't implemented (as of time of writing).

Regarding your setup for fluent and non-fluent mappings, you're almost there with your first example.

var cfg = MsSqlConfiguration.MsSql2005
  .ConnectionString.Is(_testConnectionstring)
  .ConfigureProperties(new Configuration());

cfg.AddMappingsFromAssembly(typeof(CatMap).Assembly); // loads hbm.xml files

var model = new PersistenceModel();
model.addMappingsFromAssembly(typeof(CatMap).Assembly); // loads fluent mappings
mode.Configure(cfg);

new SchemaExport(cfg).Create(true, true);

The main difference is that the SchemaExport is last. I assume your first example was actually loading the fluent mappings, but it'd already created the schema by that point.

James Gregory
A: 

You can do exactly what you want to do entirely within Fluent NHibernate.

The following code will use Fluent NHibernate syntax to fluently configure a session factory that looks for HBM (xml) mapping files, fluent mappings, and conventions from multiple possible assemblies.

var _mappingAssemblies = new Assembly[] { typeof(CatMap).Assembly };
var _autoPersistenceModel = CreateAutoPersistenceModel();
Fluently.Configure()
        .Database(MsSqlConfiguration.MsSql2005.ConnectionString(_testConnectionstring))
        .Mappings(m =>
                  {
                      foreach (var assembly in _mappingAssemblies)
                      {
                          m.HbmMappings.AddFromAssembly(assembly);
                          m.FluentMappings.AddFromAssembly(assembly)
                              .Conventions.AddAssembly(assembly);
                      }
                      m.AutoMappings.Add(_autoPersistenceModel );
                   })
        .ExposeConfiguration(c => c.SetProperty("command_timeout", "340"))
        .BuildSessionFactory();

There are many other options available to you as well: Fluent NHibernate Database Configuration

JD Courtoy