How do you figure out NHibernate.DuplicateMappingException?

131 views
Skip to first unread message

Asbjørn Ulsberg

unread,
Sep 1, 2011, 9:57:24 AM9/1/11
to Fluent NHibernate
After upgrading to NHibernate 3.2 and a build of FNH 1.3 (against NHibernate 3.2), I'm getting NHibernate.DuplicateMappingException for an entity. I've written out all mappings in the AutoPersistenceModel to file, but I can't find a duplicate. I've seen people experiencing this where `table="Entity"` is repeated too many places, but can't find this in my mappings.

How do I best debug this problem? Is there something internally in FNH I can poke at to get at the information before NHibernate throws? I find it incredibly annoying that NHibernate can't just tell me what the duplicate is, but that is just the nature of NHibernate exceptions, I guess.

--  
Asbjørn Ulsberg           -=|=-        asb...@ulsberg.no
«He's a loathsome offensive brute, yet I can't look away»

Asbjørn Ulsberg

unread,
Sep 5, 2011, 5:27:10 AM9/5/11
to Fluent NHibernate
Through the Diagnostics() method on FluentConfiguration I've figured out that for whatever reason, "ScannedSources" contains the same assembly twice and "FluentMappings" contains the class mappings from that assembly twice as well. So since "FluentMappings" contains duplicates, I guess those duplicates are fed to NHibernate which then throws.

Whether this is new behavior on FNH or NHibernate's part, I don't know, but this is code that worked in NHibernate 2.8 and FNH 1.1 that doesn't work anymore because NHibernate throws DuplicateMappingException. Any ideas how I can figure out why my mapping assembly is scanned twice and why mappings are duplicated? Wouldn't it be smart of FNH to do a .Distinct() before adding mappings to NHibernate?

--  
Asbjørn Ulsberg           -=|=-        asb...@ulsberg.no
«He's a loathsome offensive brute, yet I can't look away»

James Gregory

unread,
Sep 5, 2011, 8:11:13 AM9/5/11
to fluent-n...@googlegroups.com
Could you show your configuration code? There were some issues with the Mappings method being called multiple times before.

Asbjørn Ulsberg

unread,
Sep 5, 2011, 8:37:28 AM9/5/11
to fluent-n...@googlegroups.com
Providing my configuration code is difficult, but if there's no other way to go about this, I can of course try to create a failing test in FNH. That's going to take hours of work, though (it's hundreds of mappings), so I would love to be able to figure this out by other means first. Most of the stack trace is as follows:

--FluentConfigurationException
at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() in D:\AUL\Dev\Misc\fluent-nhibernate\src\FluentNHibernate\Cfg\FluentConfiguration.cs: line 264
at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() in D:\AUL\Dev\Misc\fluent-nhibernate\src\FluentNHibernate\Cfg\FluentConfiguration.cs: line 230
--MappingException
at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
at NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName)
at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc)
at NHibernate.Cfg.Configuration.ProcessMappingsQueue()
at NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument document)
at NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String name)
at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name)
at NHibernate.Cfg.Configuration.AddDocument(XmlDocument doc, String name)
at NHibernate.Cfg.Configuration.AddDocument(XmlDocument doc)
at FluentNHibernate.PersistenceModel.Configure(Configuration cfg) in PersistenceModel.cs: line 293
at FluentNHibernate.Automapping.AutoPersistenceModel.Configure(Configuration configuration) in D:\AUL\Dev\Misc\fluent-nhibernate\src\FluentNHibernate\Automapping\AutoPersistenceModel.cs: line 201
at FluentNHibernate.Cfg.AutoMappingsContainer.Apply(Configuration cfg, PersistenceModel model) in D:\AUL\Dev\Misc\fluent-nhibernate\src\FluentNHibernate\Cfg\AutoMappingsContainer.cs: line 86
at FluentNHibernate.Cfg.MappingConfiguration.Apply(Configuration cfg) in D:\AUL\Dev\Misc\fluent-nhibernate\src\FluentNHibernate\Cfg\MappingConfiguration.cs: line 85
at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() in D:\AUL\Dev\Misc\fluent-nhibernate\src\FluentNHibernate\Cfg\FluentConfiguration.cs: line 252
--DuplicateMappingException
at NHibernate.Cfg.Mappings.AddClass(PersistentClass persistentClass)
at NHibernate.Cfg.XmlHbmBinding.SubclassBinder.HandleSubclass(PersistentClass model, HbmSubclass subClassMapping, IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.ClassBinder.BindSubclasses(IEnumerable`1 subclasses, PersistentClass persistentClass, IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.RootClassBinder.Bind(HbmClass classSchema, IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddRootClasses(HbmClass rootClass, IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddEntitiesMappings(HbmMapping mappingSchema, IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind(HbmMapping mappingSchema)
at NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName) 

--  
Asbjørn Ulsberg           -=|=-        asb...@ulsberg.no
«He's a loathsome offensive brute, yet I can't look away»

On mandag 5. september 2011 at 14:11, James Gregory wrote:

Could you show your configuration code? There were some issues with the Mappings method being called multiple times before.

--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To view this discussion on the web visit https://groups.google.com/d/msg/fluent-nhibernate/-/kYDRnk8MgysJ.
To post to this group, send email to fluent-n...@googlegroups.com.
To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.

James Gregory

unread,
Sep 5, 2011, 9:55:06 AM9/5/11
to fluent-n...@googlegroups.com
Ok, that's fair enough. Nothing immediately obvious in the stack trace. Tell me, are you using Fluently.Configure? And are you creating multiple instances of it or doing everything within one?

Asbjørn Ulsberg

unread,
Sep 5, 2011, 10:20:05 AM9/5/11
to fluent-n...@googlegroups.com
Yea, I'm using Fluently.Configure(). The configuration code (without the mappings) looks like this:

NHibernate.Cfg.Configuration configuration = GetConfiguration();
FluentConfiguration fluentConfiguration = Fluently.Configure(configuration);
fluentConfiguration.Diagnostics(SetupDiagnostics);

AddListenersTo(configuration);

fluentConfiguration.Mappings(ConfigureMapping);

try
{
this.sessionFactory = fluentConfiguration.BuildSessionFactory();
this.configured = true;
}
catch (Exception exception)
{
throw;
}

It's the BuildSessionFactory() that fails, and even though it's only invoked once, the Diagnostics() stuff is invoked twice; first without much data at all, second with too much (duplicate assembly and fluent mappings).

--  
Asbjørn Ulsberg           -=|=-        asb...@ulsberg.no
«He's a loathsome offensive brute, yet I can't look away»

On mandag 5. september 2011 at 15:55, James Gregory wrote:

Ok, that's fair enough. Nothing immediately obvious in the stack trace. Tell me, are you using Fluently.Configure? And are you creating multiple instances of it or doing everything within one?

--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To view this discussion on the web visit https://groups.google.com/d/msg/fluent-nhibernate/-/aLPhyYFU0gwJ.

Asbjørn Ulsberg

unread,
Sep 5, 2011, 10:35:23 AM9/5/11
to fluent-n...@googlegroups.com
Not sure how valuable it is, but this is the call stack for the two different times my diagnostics code is invoked:

DiagnosticListener.Format(FluentNHibernate.Diagnostics.DiagnosticResults results) Line 127
DiagnosticListener.Receive.AnonymousMethod__0(Common.Logging.FormatMessageHandler f) Line 35 + 0x11 bytes
DiagnosticListener.Receive(FluentNHibernate.Diagnostics.DiagnosticResults results) Line 35 + 0x33 bytes
FluentNHibernate.DLL!FluentNHibernate.Diagnostics.DefaultDiagnosticMessageDespatcher.Publish(FluentNHibernate.Diagnostics.DiagnosticResults results) Line 18 + 0xc bytes
FluentNHibernate.DLL!FluentNHibernate.Diagnostics.DefaultDiagnosticLogger.Flush() Line 28 + 0xf bytes
FluentNHibernate.DLL!FluentNHibernate.PersistenceModel.BuildMappings() Line 171 + 0xc bytes
FluentNHibernate.DLL!FluentNHibernate.Automapping.AutoPersistenceModel.BuildMappings() Line 136 + 0x8 bytes
FluentNHibernate.DLL!FluentNHibernate.PersistenceModel.EnsureMappingsBuilt() Line 221 + 0xe bytes
FluentNHibernate.DLL!FluentNHibernate.PersistenceModel.Configure(NHibernate.Cfg.Configuration cfg) Line 277 + 0x8 bytes
FluentNHibernate.DLL!FluentNHibernate.Automapping.AutoPersistenceModel.Configure(NHibernate.Cfg.Configuration configuration) Line 201 + 0xb bytes
FluentNHibernate.DLL!FluentNHibernate.Cfg.AutoMappingsContainer.Apply(NHibernate.Cfg.Configuration cfg, FluentNHibernate.PersistenceModel model) Line 86 + 0xb bytes
FluentNHibernate.DLL!FluentNHibernate.Cfg.MappingConfiguration.Apply(NHibernate.Cfg.Configuration cfg) Line 85 + 0x1e bytes
FluentNHibernate.DLL!FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() Line 252 + 0x2a bytes
FluentNHibernate.DLL!FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() Line 230 + 0x8 bytes
Configurator.DoConfigure(System.Func<NHibernate.Cfg.Configuration,NHibernate.Cfg.Configuration> configurationFunc) Line 120 + 0x13 bytes
Configurator.Configure() Line 183 + 0xd bytes
PersistenceModelBaseTests.AfterConfiguring_TypesAreExcludedInAutomappingConfiguration() Line 59 + 0x8 bytes

And:

DiagnosticListener.Format(FluentNHibernate.Diagnostics.DiagnosticResults results) Line 127
DiagnosticListener.Receive.AnonymousMethod__0(Common.Logging.FormatMessageHandler f) Line 35 + 0x11 bytes
DiagnosticListener.Receive(FluentNHibernate.Diagnostics.DiagnosticResults results) Line 35 + 0x33 bytes
FluentNHibernate.DLL!FluentNHibernate.Diagnostics.DefaultDiagnosticMessageDespatcher.Publish(FluentNHibernate.Diagnostics.DiagnosticResults results) Line 18 + 0xc bytes
FluentNHibernate.DLL!FluentNHibernate.Diagnostics.DefaultDiagnosticLogger.Flush() Line 28 + 0xf bytes
FluentNHibernate.DLL!FluentNHibernate.PersistenceModel.BuildMappings() Line 171 + 0xc bytes
FluentNHibernate.DLL!FluentNHibernate.PersistenceModel.EnsureMappingsBuilt() Line 221 + 0xe bytes
FluentNHibernate.DLL!FluentNHibernate.PersistenceModel.Configure(NHibernate.Cfg.Configuration cfg) Line 277 + 0x8 bytes
FluentNHibernate.DLL!FluentNHibernate.Cfg.MappingConfiguration.Apply(NHibernate.Cfg.Configuration cfg) Line 87 + 0xe bytes
MappingConfigurationWrapper.AddTypesToExcludeFromAutoMappingTo(PersistenceModelList persistenceModels) Line 120 + 0x10 bytes
Configurator.ConfigureMapping(FluentNHibernate.Cfg.MappingConfiguration mappingConfiguration) Line 194 + 0xd bytes
FluentNHibernate.DLL!FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration() Line 250 + 0xe bytes
FluentNHibernate.DLL!FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() Line 230 + 0x8 bytes
Configurator.DoConfigure(System.Func<NHibernate.Cfg.Configuration,NHibernate.Cfg.Configuration> configurationFunc) Line 120 + 0x13 bytes
Configurator.Configure() Line 183 + 0xd bytes
PersistenceModelBaseTests.AfterConfiguring_TypesAreExcludedInAutomappingConfiguration() Line 59 + 0x8 bytes

It might be the intended behavior that my IDiagnosticListener is invoked twice for the same code path, so the above might of course be useless information.

--  
Asbjørn Ulsberg           -=|=-        asb...@ulsberg.no
«He's a loathsome offensive brute, yet I can't look away»

James Gregory

unread,
Sep 7, 2011, 4:24:09 AM9/7/11
to fluent-n...@googlegroups.com
The two stack traces show that the first is running through the automapper, and the second just a regular persistencemodel. I can't remember this part off the top of my head, but it does sound like it's intentional.

It's quite difficult to give you any more help without either seeing how you're adding the mappings, or getting my hands on a reproduction. FNH *should* prevent duplicates from being added to NH, but there's obviously a hole in that defense; if you're seeing the same assembly in ScannedSources multiple times, then it sounds like you're adding it twice (or we've got a bug!). Again, hard to tell which without actually seeing something, I'm afraid.

Isaac Cambron

unread,
Sep 7, 2011, 5:02:05 PM9/7/11
to fluent-n...@googlegroups.com
I know of at least one other hole in the duplicate mapping defense, which may or may not be the same as what you're seeing. I'm planning on pinpointing and fixing that one 'cause it's bugging me, but if can get me something reproduceable, I'll look at yours while I'm there.

On Wed, Sep 7, 2011 at 4:24 AM, James Gregory <jagreg...@gmail.com> wrote:
The two stack traces show that the first is running through the automapper, and the second just a regular persistencemodel. I can't remember this part off the top of my head, but it does sound like it's intentional.

It's quite difficult to give you any more help without either seeing how you're adding the mappings, or getting my hands on a reproduction. FNH *should* prevent duplicates from being added to NH, but there's obviously a hole in that defense; if you're seeing the same assembly in ScannedSources multiple times, then it sounds like you're adding it twice (or we've got a bug!). Again, hard to tell which without actually seeing something, I'm afraid.

--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To view this discussion on the web visit https://groups.google.com/d/msg/fluent-nhibernate/-/f6EHwxskGB4J.

Asbjørn Ulsberg

unread,
Sep 26, 2011, 8:39:01 AM9/26/11
to fluent-n...@googlegroups.com
Sorry for the late reply, but I've been swamped in figuring this out. I've finally moved forward by doing lots of de-duplication in the code I have wrapped around Fluent NHibernate. Whether any of this code should be in FNH itself is difficult to answer, but my fork of FNH contains the updates I've done. I don't think any of them are relevant to fixing the problem, though.

So, I haven't really been able to reproduce the problem in the FNH codebase, but if you fix these problems in FNH, I'll be sure to test them out at least! Thanks!

--  
Asbjørn Ulsberg           -=|=-        asb...@ulsberg.no
«He's a loathsome offensive brute, yet I can't look away»
Reply all
Reply to author
Forward
0 new messages