Fluent NHibernate no data being returned - nhibernate

I have been successfully using NHibernate, but now I am trying to move to Fluent NHibernate. I have created all of my mapping files and set up my session manager to use a Fluent Configuration. I then run my application and it runs successfully, but no data is returned.
There are no errors or any indication that there is a problem, but nothing runs.
when using NHibernate, if I don't set my hbm xml files as an embedded resource, this same thing happens. This makes me wonder what I have to set my Map classes to. Right now, they are just set to Compile, and they are compiled into the dll, which I can see by disassembling it.
Does anyone have any thoughts as to what may be happening here?
Thanks
private ISessionFactory GetSessionFactory()
{
return Fluently.Configure()
.Database(
IfxOdbcConfiguration
.Informix1000
.ConnectionString("Provider=Ifxoledbc.2;Password=mypass;Persist Security Info=True;User ID=myuser;Data Source=mysource")
.Dialect<InformixDialect1000>()
.ProxyFactoryFactory<ProxyFactoryFactory>()
.Driver<OleDbDriver>()
.ShowSql()
)
.Mappings(
x => x.FluentMappings.AddFromAssembly(System.Reflection.Assembly.GetExecutingAssembly())
//.ExportTo("C:\\mappings")
)
.BuildSessionFactory();
}

Does the executing assembly contain the fluent mapping classes? I would try:
.Mappings(x => x.FluentMappings.AddFromAssemblyOf<MappedType>())
Where MappedType is a class that has a fluent mapping.

They should just be set to compile, that's fine. Nothing special needed here. The problem is most likely in your fluent configuration rather than the mapping.

Related

Fluent NHibernate Syscache2 cache expiration

I've been working on a WCF service that uses fluent and syscache2. I've pretty much read every article on SO regarding my current dilemma; I've had no luck.
I am trying to set the expiration time for my second-level cache. Whatever value I set seems to be ignored and the default value of 5 minutes is used to expire the cache.
Fluent configuration:
Note: contextClass is just a descriptor class holding values passed to the configuration.
var cfg = Fluently.Configure()
.Database(
MsSqlConfiguration.MsSql2008
.ConnectionString(c => c.Is(connectionString))
.ShowSql()
)
.Diagnostics(d => d.Enable())
.Cache(c => c
.UseQueryCache()
.ProviderClass(typeof(NHibernate.Caches.SysCache2.SysCacheProvider).AssemblyQualifiedName))
.Mappings(m => m
.FluentMappings
.AddFromAssembly(assembly))
.ExposeConfiguration(x =>
{
x.SetProperty(NHibernate.Cfg.Environment.CurrentSessionContextClass, contextClass.Id);
x.SetProperty(NHibernate.Cfg.Environment.PrepareSql, contextClass.PrepareSql); //set prepare_sql true/false
x.SetProperty(NHibernate.Cfg.Environment.CacheDefaultExpiration, contextClass.ExpireL2Cache); //set default expiration in seconds
});
I also have the app.config file set up as following:
<configSections>
<section name="syscache" type="NHibernate.Caches.SysCache2.SysCacheSection, NHibernate.Caches.SysCache2"/>
</configSections>
<syscache>
<cache expiration="600" priority="5" />
</syscache>
There was a variant of the app.config which had a syscache section that used regions but that didn't work either.
Anyone have any suggestions on ideas?
Thanks
I've always used this without problems:
.ExposeConfiguration (cfg => {
cfg.Properties.Add ("expiration", "900");
})
Not sure if Properties.Add behaves any differently than the SetProperty call you're using though.
It seems like if you're using a newer version of NHibernate you can lean on the new extension methods in the NHibernate.Cfg namespace for this as well (this would replace your entire .Cache call in fluent)
.ExposeConfiguration (cfg => {
cfg.SessionFactory().Caching.Through<SysCacheProvider>().WithDefaultExpiration(900);
})
Doing some reading I found this:
cache.default_expiration or expiration (Int32): since NH Contrib 2.1 cache.default_expiration is the new setting name that should be used instead of expiration to specify number of seconds after which the cache item must be invalidated. Default value is 300 seconds. The old name is still supported for backward compatibility.
So the property name is probably not your issue (wondering now if the "expiration" key that I used was maybe specific to the memcache provider as well, though it seemed to work with syscache).

S#arp Architecture 1.9 + Fluently.Configure() NHiberbante

I have a need to fluently configure nhibernate in my S#arp application so that I can use a custom NHibernate.Search directory for each of my tenants in a multi-tenant app.
However I have googled for hours looking for a solution but can't seem to find anything current that works.
Thanks,
Paul
I haven't tried this myself, but AddConfiguration takes a dictionary of cfgProperties, which I guess you can pass the tenant specific hibernate.search.default.indexBase value to.
I had a look at this, adding the key as described above will cause a problem if you attempt to use CfgHelper.LoadConfiguration() since it will return null.
But you can configure NHSearch to use different directories for each factory using the factory key:
<nhs-configuration xmlns="urn:nhs-configuration-1.0">
<search-factory sessionFactoryName="YOUR_TENANT1_FACTORY_KEY">
<property name="hibernate.search.default.indexBase">~\IndexTenant1</property>
</search-factory>
<search-factory sessionFactoryName="YOUR_TENANT2_FACTORY_KEY">
<property name="hibernate.search.default.indexBase">~\Tenant2</property>
</search-factory>
</nhs-configuration>
If you are following instructions on
http://wiki.sharparchitecture.net/Default.aspx?Page=NHibSearch
You would need to change the method GetIndexDirectory to
private string GetIndexDirectory() {
INHSConfigCollection nhsConfigCollection = CfgHelper.LoadConfiguration();
string factoryKey = SessionFactoryAttribute.GetKeyFrom(this); // Change this with however you get the factory key for your tenants,
string property = nhsConfigCollection.GetConfiguration(factoryKey).Properties["hibernate.search.default.indexBase"];
var fi = new FileInfo(property);
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fi.Name);
}

S#arp Architecture - Rhino Security (unmapped class: Rhino.Security.IUser)

I'm using S#arp Architecture 1.6 and have implemented the Rhino Security integration as per
Rhino Security - S#arp Architecture
I'm using the latest build from Rhino.Commons
My Application_EndRequest method contains
ISession session = NHibernateSession.Current;
My ComponentRegister.cs contains
container.Kernel.Register(
Component.For<IAuthorizationService>()
.ImplementedBy<AuthorizationService>()
.LifeStyle.Is(LifestyleType.Transient),
Component.For<IAuthorizationRepository>()
.ImplementedBy<AuthorizationRepository>()
.LifeStyle.Is(LifestyleType.Transient),
Component.For<IPermissionsBuilderService>()
.ImplementedBy<PermissionsBuilderService>()
.LifeStyle.Is(LifestyleType.Transient),
Component.For<IPermissionsService>()
.ImplementedBy<PermissionsService>()
.LifeStyle.Is(LifestyleType.Transient),
Component.For<IUnitOfWorkFactory>()
.ImplementedBy<NHibernateUnitOfWorkFactory>()
.LifeStyle.Is(LifestyleType.Singleton),
Component.For<Rhino.Commons.IRepository<User>>()
.ImplementedBy<NHRepository<User>>()
.LifeStyle.Is(LifestyleType.Transient)
);
container.AddFacility<FactorySupportFacility>()
.Register(Component.For<ISession>()
.UsingFactoryMethod(() => NHibernateSession.Current)
.LifeStyle.Is(LifestyleType.Transient));
I have also added RhinoSecurityPersistenceConfigurer() as per instructions.
The error I'm recieving on calling
UnitOfWork.Start()
is
An association from the table Permissions refers to an unmapped class: Rhino.Security.IUser
Does anyone know what the cause of this error may be?
Has anyone successfully integrated Rhino.Security with S#arp Architecture?
Any help would be great.
Thanks
Rich
-- Additional Details --
Thanks for all the replies so far.
I've still not been able to resolve this, so thought I'd add more details.
In my Global.asax.cs I have
private void InitializeNHibernateSession()
{
NHibernateSession.Init(
webSessionStorage,
new string[] { Server.MapPath("~/bin/SwitchSnapshot.Data.dll") },
new AutoPersistenceModelGenerator().Generate(),
Server.MapPath("~/NHibernate.config"),
null, null, new RhinoSecurityPersistenceConfigurer());
}
RhinoSecurityPersistenceConfigurer :
public Configuration ConfigureProperties(Configuration nhibernateConfig)
{
Security.Configure<User>(nhibernateConfig, SecurityTableStructure.Prefix);
return nhibernateConfig;
}
I have an AuthorizationAttribute which calls
using (UnitOfWork.Start())
The error is occuring in NHibernateUnitOfWorkFactory.cs as
sessionFactory = cfg.BuildSessionFactory();
You need an NHibernate mapping for your User class (i.e. the class that implements the IUser interface). You also need a table in the database with the correct fields for your User class.
You have to let RS do some configuration work before the SessionFactory is created. Look at the second issue here http://groups.google.com/group/sharp-architecture/browse_frm/thread/4093c52596f54d23/194f19cd08c8fdd7?q=#194f19cd08c8fdd7. It should get you in the right direction.
Thanks to all who helped out.
In the end it was my own fault.
All I needed to do was to follow the S#arp Architecture Instructions a little better.
From an old version of S#arp I had 2 config files hibernate.cfg.xml and NHibernate.config. I thought I still needed both, but all I needed was hibernate.cfg.xml for S#arp version 1.6 and mapped User.cs using Fluent NHibernate.
Other changes I did was in ComponentRegister.cs
container.Kernel.Register(
Component.For<IAuthorizationService>()
.ImplementedBy<AuthorizationService>()
.LifeStyle.Is(LifestyleType.Transient),
Component.For<IAuthorizationRepository>()
.ImplementedBy<AuthorizationRepository>()
.LifeStyle.Is(LifestyleType.Transient),
Component.For<IPermissionsBuilderService>()
.ImplementedBy<PermissionsBuilderService>()
.LifeStyle.Is(LifestyleType.Transient),
Component.For<IPermissionsService>()
.ImplementedBy<PermissionsService>()
.LifeStyle.Is(LifestyleType.Transient),
Component.For<IUnitOfWorkFactory>()
.ImplementedBy<NHibernateUnitOfWorkFactory>()
.LifeStyle.Is(LifestyleType.Singleton),
Component.For<Rhino.Commons.IRepository<User>>()
.ImplementedBy<NHRepository<User>>()
.LifeStyle.Is(LifestyleType.Transient)
);
container.Kernel.AddFacility<FactorySupportFacility>()
.Register(Component.For<ISession>()
.UsingFactoryMethod(() => NHibernateSession.Current)
.LifeStyle.Is(LifestyleType.Transient)
);
Then use the following in my code.
var authorizationService = IoC.Resolve<IAuthorizationService>();
using (UnitOfWork.Start())
{
}

Fluent config not generating mapping files

I am trying to get Fluent nHibernate to generate mappings so I can take a look at the files and the sql.
My code is based on this post and on what I can glean from the documentation.
Fluent mapping - entities and classmaps in different assemblies
I am using the latest code from git.
Here’s my config code:
Configuration cfg = new Configuration();
var ft = Fluently.Configure(cfg);
//DbConnection by fluent
ft.Database
(
MsSqlConfiguration
.MsSql2008
.ConnectionString("……")
.ShowSql()
.UseReflectionOptimizer()
);
//get mapping files.
ft.Mappings(m =>
{
//set up the mapping locations
m.FluentMappings.AddFromAssemblyOf<Entity>()
.ExportTo(#"C:\temp");
m.Apply(cfg);
});
I also tried:
var sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration
.MsSql2008
.ShowSql()
.ConnectionString(“……"))
.Mappings(p => p.FluentMappings
.AddFromAssemblyOf<Entity>()
.ExportTo(#"c:\temp\"))
.BuildSessionFactory();
I have verified that the connection string is correct.
The issue is that no mapping files show up in the ExportTo folder and no sql code shows up in the output window or in the log file. No errors or exceptions are generated either.
I have no idea where to go from here.
Thank you in advance.
Rick
I think you have to actually spin up some objects to get the maps written out. If I remember correctly, this is not done at config time.

how to set generate_statistics = true with fluent nhibernate

From what I understand I need to end up with this
<property name="hibernate.generate_statistics">true</property>
on the session factory configuration, but I've no idea how to do that with fluent nhibernate.
Configuration.ExposeConfiguration(c => c.SetProperty("generate_statistics", "true"));
In NHibernate 3 to avoid strings:
Configuration.ExposeConfiguration(c => c.SetProperty(NHibernate.Cfg.Environment.GenerateStatistics, "true"));
Depending on how you're configuring Fluent NHibernate, the Database Configuration has a Raw method that you can use to specify settings that we haven't implemented yet.
SQLiteConfiguration.Standard
.Raw("hibernate.generate_statistics", "true");