I need to dynamically add named queries to the NHibernate configuration object. The search engines return few hits referencing NamedSQLQueryDefinition or NamedQueryDefinition. Below is what I'm attempting to do. I don't know what to provide to the NamedSQLQueryDefinition constructor to successfully create a query. Can anyone provide a sample of how to create a new NamedSQLQueryDefinition the right way? Thanks!!
Session initializer:
private static ISessionFactory CreateSessionFactory()
{
var configuration = new Configuration();
return Fluently.Configure(configuration.Configure())
.ExposeConfiguration(AddQueries)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>())
.Mappings(m => m.HbmMappings.AddFromAssemblyOf<Program>())
.BuildConfiguration()
.BuildSessionFactory();
}
The AddQueries would look something like this:
private static void AddQueries(Configuration cfg)
{
var nameQuery = new NamedSQLQueryDefinition("exec pr_GETCustomer ?", ...)
cfg.NamedSQLQueries.Add("pr_GETCustomer", nameQuery);
var cust = cfg.GetClassMapping(typeof (Customer));
cust.LoaderName = "pr_GETCustomer";
}
PS: I'm trying this route because Fluent NHibernate does not implement a way to configuring the loader & sql-query elements from the hbm file.