Set default max_lo for NHibernate HiLo generator? - nhibernate

Is there any way to set a default value for max_lo that will take effect for all mapped entities? All of my entities are currently mapped via Xml. I know the default is 32678, but I would like to reduce this to 1000.
I've had a look through the NH configuration xsd and I can't see any settings in there. I think that you should be able to achieve this ok if you are mapping by code, but I am currently using Xml and don't fancy changing across.
Thanks.

you can also override the value on SessionFactory generation, which is only done once:
private void InitSessionFactory()
{
var cfg = new Configuration().Configure();
foreach (var cm in cfg.ClassMappings) {
if (cm.Identifier.IsSimpleValue) {
var simpleVal = cm.Identifier as SimpleValue;
if (simpleVal.IdentifierGeneratorStrategy == "hilo"){
simpleVal.IdentifierGeneratorProperties["max_lo"] = "1000";
}
}
}
sessionFactory = cfg.BuildSessionFactory();
}
this NH2 code so for NH3 there might be some differences

No way to configure default/global from hbm. Int16.MaxValue is simply hardcoded in NHibernate (as of 3.2). TableHiLoGenerator source:
public override void Configure(IType type, ...)
{
...
maxLo = PropertiesHelper.GetInt64(MaxLo, parms, Int16.MaxValue);
...
}
I guess you can open feature request here.

It looks like it may be possible to do this by extending the NH hilo generator as per http://daniel.wertheim.se/2011/03/08/nhibernate-custom-id-generator/

Related

How to set NHibernate session to eager fetch

I've got a method helper in my tests base class that looks like this:
protected TEntity Fetch<TEntity>(Guid id) where TEntity : Entity
{
using (var session = GetSession())
return session.Get<TEntity>(id);
}
So I can call it from an integration test as such:
var persistedFoo = Fetch<Foo>(foo.Id);
How can I set the session in my Fetch method to eager fetch all properties in TEntity?
According to NHibernate docs here you should use NHibernateUtility class, so change your code into something like this should help:
using(var session = GetSession())
{
var entity = session.Get<TEntity>(id);
NHibernateUtil.Initialize(entity);
}
alternatively, you can use one of nHib's querying APIs (I personally prefer QueryOver), to do something like
session.QueryOver<Cat>().Where(cat => cat.Id == id).Fetch(c => c.Kittens).Eager.
This gives you the added bonus of controlling exactly which properties / collections would be fetched.
Also, it's recommended that you do NOT abstract-away your ISession usage in repositories.
It would prevent you from benefiting from such nHibernate features as batching (see ayende's post here)

Raven DB: How can I delete all documents of a given type

More specifically in Raven DB, I want to create a generic method with a signature like;
public void Clear<T>() {...
Then have Raven DB clear all documents of the given type.
I understand from other posts by Ayende to similar questions that you'd need an index in place to do this as a batch.
I think this would involve creating an index that maps each document type - this seems like a lot of work.
Does anyone know an efficient way of creating a method like the above that will do a set delete directly in the database?
I assume you want to do this from the .NET client. If so, use the standard DocumentsByEntityName index:
var indexQuery = new IndexQuery { Query = "Tag:" + collectionName };
session.Advanced.DocumentStore.DatabaseCommands.DeleteByIndex(
"Raven/DocumentsByEntityName",
indexQuery,
new BulkOperationOptions { AllowStale = true });
var hilo = session.Advanced.DocumentStore.DatabaseCommands.Get("Raven/H‌​ilo/", collectionName);
if (hilo != null) {
session.Advanced.DocumentStore.DatabaseCommands.Delete(hilo.‌​Key, hilo.Etag);
}
Where collectionName is the actual name of your collection.
The first operation deletes the items. The second deletes the HiLo file.
Also check out the official documentation - How to delete or update documents using index.
After much experimentation I found the answer to be quite simple, although far from obvious;
public void Clear<T>()
{
session.Advanced.DocumentStore.DatabaseCommands.PutIndex(indexName, new IndexDefinitionBuilder<T>
{
Map = documents => documents.Select(entity => new {})
});
session.Advanced.DatabaseCommands.DeleteByIndex(indexName, new IndexQuery());
}
Of course you almost certainly wouldn't define your index and do your delete in one go, I've put this as a single method for the sake of brevity.
My own implementation defines the indexes on application start as recommended by the documentation.
If you wanted to use this approach to actually index a property of T then you would need to constrain T. For example if I have an IEntity that all my document classes inherit from and this class specifies a property Id. Then a 'where T : IEntity' would allow you to use that property in the index.
It's been said in other places, but it's also worth noting that once you define a static index Raven will probably use it, this can cause your queries to seemingly not return data that you've inserted:
RavenDB Saving to disk query
I had this problem as well and this is the solution that worked for me. I'm only working in a test project, so this might be slow for a bigger db, but Ryan's answer didn't work for me.
public static void ClearDocuments<T>(this IDocumentSession session)
{
var objects = session.Query<T>().ToList();
while (objects.Any())
{
foreach (var obj in objects)
{
session.Delete(obj);
}
session.SaveChanges();
objects = session.Query<T>().ToList();
}
}
You can do that using:
http://blog.orangelightning.co.uk/?p=105

Is it possible to get a list of all mapped entities from NHibernate IStatelessSession or ISession?

I am trying to write a test for my NHibernate mappings that will automatically pick up and test any new mappings that get added.
At the moment I have a test that opens a session to a known test database then attempts to load the first entity of each type and asserts that it is not null.
This all works fine but it means that every time I add a new entity mapping, I need to remember to update the test.
So, what I want to do is to inspect the mappings and try to load one of each of the mapped entities, but the NHibernate Configuration object that the sessionfactory is built from is not visible to my test so I was wondering if there is a way to access a list of mapped entities from the session or do I need to expose the original Configuration instead?
You can get SessionFactory from Session and SessionFactory has method GetAllClassMetadata() which returns list of IClassMetadata. And from IClassMetadata you can get MappedClass (GetMappedClass())
But you will need some extra work to get subclasses. This code can help:
var metaData = this.session.SessionFactory.GetClassMetadata(baseClass);
if (metaData != null && metaData.HasSubclasses)
{
foreach (string entityName in ((NHibernate.Persister.Entity.IEntityPersister)metaData).EntityMetamodel.SubclassEntityNames)
{
var metadata = this.session.SessionFactory.GetClassMetadata(entityName);
result.Add(metadata.GetMappedClass(EntityMode.Poco));
}
}
I expose the configuration object and do a mapping that queries all of my entities like this. It will output all errors from each of my mappings.:
[TestMethod()]
public void AllNHibernateMappingsAreOkay()
{
bool failed = false;
log4net.Config.XmlConfigurator.Configure();
using (ISession session = SessionFactory.GetCurrentSession())
{
foreach (var s in SessionFactory.GetConfig().ClassMappings)
{
try
{
SessionFactory.GetCurrentSession().CreateQuery(string.Format("from {0} e", s.MappedClass.Name))
.SetFirstResult(0).SetMaxResults(50).List();
}
catch (Exception ex)
{
failed = true;
log.ErrorFormat("\r\n\r\n {0} \r\n {1} \r\n\r\n", ex.Message, ex.InnerException.Message);
}
}
}
Assert.IsFalse(failed, "One or more mappings have errors in them. Please refer to output or logs.");
}
if you have only one row per entity then you could issue session.QueryOver<object>().List();

How to export hbm xml files using s#arparchitecture with fluent mappings

This question was asked before but the answers all show how to export the hbm files from fluentnhibernate. We are using S#arpArchitecture which wraps fluent. I am able to export the schema but what I really want is the xml files to troubleshoot errors. I've done this using FNH before but adding S#arp to the mix has complicated things where I cannot figure it out.
I've found this question asked on several forums, but I can't find one that shows how to get the mapping files.
Here is how I do it in one of my projects:
[TestMethod]
public void CreateSchema()
{
var mappingOutput = ConfigurationManager.AppSettings["xmlMappingOutputDirectory"];
var sqlOutput = ConfigurationManager.AppSettings["sqlOutputDirectory"];
Configuration cfg = new Configuration().Configure();
var persistenceModel = new PersistenceModel();
persistenceModel.AddMappingsFromAssembly(Assembly.Load("ProjectName.Data"));
persistenceModel.Configure(cfg);
persistenceModel.WriteMappingsTo(mappingOutput);
new SchemaExport(cfg).SetOutputFile(sqlOutput).Create(true, false);
}
You will need to set the two keys in the your app config or provide values directly for them.
http://wiki.fluentnhibernate.org/Fluent_configuration#Exporting_mappings
In the Mappings call, you can do the following:
.Mappings(m =>
{
m.FluentMappings
.AddFromAssemblyOf<YourEntity>()
.ExportTo(#"C:\your\export\path");
m.AutoMappings
.Add(/* ... */)
.ExportTo(#"C:\your\export\path");
})
As it turns out that only works if you're not using automapping. Here's the solution if you're using automapping:
public void CanGenerateMappingFiles()
{
DirectoryInfo directoryInfo = new DirectoryInfo("../../../../db/mappings");
if (!directoryInfo.Exists)
directoryInfo.Create();
Configuration cfg = new Configuration().Configure();
var autoPersistenceodel = new AutoPersistenceModelGenerator().Generate();
autoPersistenceodel.Configure(cfg);
autoPersistenceodel.AddMappingsFromAssembly(Assembly.Load("TrackerI9.Data"));
autoPersistenceodel.WriteMappingsTo(directoryInfo.FullName);
}
You'll have to make sure that your configuration is set up correctly and that you choose an appropriate location for the directory, but otherwise this should work. It did for me.

SharpArchitecture: Using FNH's ClassMaps instead of auto mapping

I need to use ClassMaps instead of auto mapping because of legacy database. But I don't see how to tune SharpArch to use them. I tried to remove AutoPersistentModelGenerator and use the following code in the InitializeNHibernateSession method:
var config = NHibernateSession.Init(webSessionStorage,
new[]{"ApplicationConfiguration.Models.dll"});
Fluently.Configure(config)
.Mappings(m =>
{
m.FluentMappings.AddFromAssemblyOf<ConfigSchema>();
});
But I always get MappingException - "No persister for: ConfigSchema" when trying to work with the ConfigSchema.
Has anyone tried to do this?
Edit:
ConfigSchema is a part of domain model.
I'm stupid. Fluently.Configure(config) generates a new config for NHibernate. So it will never be used in my scenario. All I was need is to use the following code in the AutoPersistentModelGenerator:
public AutoPersistenceModel Generate()
{
var mappings = new AutoPersistenceModel();
mappings.AddMappingsFromAssembly(typeof(ConfigVersionMap).Assembly);
return mappings;
}
I'm not all that familiar with the S#arp project, but is ConfigSchema a type from you domain model? The generic argument T to AddFromAssemblyOf<T> should be a mapped class from your domain model.