mapping causing a FluentConfigurationException

2,407 views
Skip to first unread message

Berryl Hesh

unread,
Jul 5, 2009, 7:27:03 PM7/5/09
to Fluent NHibernate
Hello:

I am trying to work out a mapping that is tricky (for me), and I'm
getting a FluentConfigurationException when I try to generate DDL. I
do not understand what the exception is saying well enough to try and
fix the mappings from it though.

Can someone please clarify that? I'm happy to lay out the mappings to
help figure this out, of course, but that would be a much longer and
noisier posting, and maybe unnecessary if you've seen the exception
before.

I'm using the S#rpArch framework, although I tried swapping in the
latest FNH release (rev 539) before posting this. I do not get the
error if I comment out two of my class mappings. Here is the full
error:

FluentNHibernate.Cfg.FluentConfigurationException: An invalid or
incomplete configuration was used while creating a SessionFactory.
Check PotentialReasons collection, and InnerException for more detail.

* Database was not configured through Database method.
---> FluentNHibernate.Cfg.FluentConfigurationException: An invalid
or incomplete configuration was used while creating a SessionFactory.
Check PotentialReasons collection, and InnerException for more detail.

* Database was not configured through Database method.
---> NHibernate.MappingException: (XmlDocument)(3,6): XML validation
error: The element 'class' in namespace 'urn:nhibernate-mapping-2.2'
has invalid child element 'property' in namespace 'urn:nhibernate-
mapping-2.2'. List of possible elements expected: 'meta, subselect,
cache, synchronize, comment, tuplizer, id, composite-id' in namespace
'urn:nhibernate-mapping-2.2'. --->
System.Xml.Schema.XmlSchemaValidationException: The element 'class' in
namespace 'urn:nhibernate-mapping-2.2' has invalid child element
'property' in namespace 'urn:nhibernate-mapping-2.2'. List of possible
elements expected: 'meta, subselect, cache, synchronize, comment,
tuplizer, id, composite-id' in namespace 'urn:nhibernate-mapping-2.2'.
--- End of inner exception stack trace ---
at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
at NHibernate.Cfg.Configuration.ValidationHandler(Object o,
ValidationEventArgs args)
at System.Xml.Schema.XmlSchemaValidator.SendValidationEvent
(ValidationEventHandler eventHandler, Object sender,
XmlSchemaValidationException e, XmlSeverityType severity)
at System.Xml.Schema.XmlSchemaValidator.ElementValidationError
(XmlQualifiedName name, ValidationState context,
ValidationEventHandler eventHandler, Object sender, String sourceUri,
Int32 lineNo, Int32 linePos, Boolean getParticles)
at System.Xml.Schema.XmlSchemaValidator.ValidateElementContext
(XmlQualifiedName elementName, Boolean& invalidElementInContext)
at System.Xml.Schema.XmlSchemaValidator.ValidateElement(String
localName, String namespaceUri, XmlSchemaInfo schemaInfo, String
xsiType, String xsiNil, String xsiSchemaLocation, String
xsiNoNamespaceSchemaLocation)
at System.Xml.XsdValidatingReader.ProcessElementEvent()
at System.Xml.XsdValidatingReader.ProcessReaderEvent()
at System.Xml.XsdValidatingReader.Read()
at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader,
Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at NHibernate.Cfg.Configuration.LoadMappingDocument(XmlReader
hbmReader, String name)
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)
at FluentNHibernate.AutoMap.AutoPersistenceModel.Configure
(Configuration configuration)
at FluentNHibernate.Cfg.AutoMappingsContainer.Apply(Configuration
cfg)
at FluentNHibernate.Cfg.MappingConfiguration.Apply(Configuration cfg)
at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration()
--- End of inner exception stack trace ---
at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration()
at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory()

* Database was not configured through Database method.

--- End of inner exception stack trace ---
at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory()
C:\MyStuff\Dev\Projects\SharpArchitecture\src\SharpArch\SharpArch.Data
\NHibernate\NHibernateSession.cs(110,0): at
SharpArch.Data.NHibernate.NHibernateSession.CreateSessionFactoryFor
(String[] mappingAssemblies, AutoPersistenceModel
autoPersistenceModel, Configuration cfg, IPersistenceConfigurer
persistenceConfigurer)
C:\MyStuff\Dev\Projects\SharpArchitecture\src\SharpArch\SharpArch.Data
\NHibernate\NHibernateSession.cs(75,0): at
SharpArch.Data.NHibernate.NHibernateSession.Init(ISessionStorage
storage, String[] mappingAssemblies, AutoPersistenceModel
autoPersistenceModel, String cfgFile, IDictionary`2 cfgProperties,
String validatorCfgFile, IPersistenceConfigurer persistenceConfigurer)
C:\MyStuff\Dev\Projects\SharpArchitecture\src\SharpArch\SharpArch.Data
\NHibernate\NHibernateSession.cs(46,0): at
SharpArch.Data.NHibernate.NHibernateSession.Init(ISessionStorage
storage, String[] mappingAssemblies, AutoPersistenceModel
autoPersistenceModel, String cfgFile)
C:\Users\BBH\Documents\Visual Studio 2008\Projects\TTS\src\Core.Data
\Bootstrap\Bootstrapper.cs(72,0): at
TTS.Core.Data.Bootstrap.Bootstrapper.InitNHib(AutoPersistenceModel
autoPersistenceModel, String factoryKey)
C:\Users\BBH\Documents\Visual Studio 2008\Projects\TTS\src
\ConstructionAdmin.Tests\Data\Production\TTS_Bootstrapper.cs(36,0): at
TTS.ConstructionAdmin.Tests.Data.Production.TTS_Bootstrapper.BootstrapNHibernate
()
C:\Users\BBH\Documents\Visual Studio 2008\Projects\TTS\src
\ConstructionAdmin.Tests\Data\Production\TTS_BootstrapperTests.cs
(46,0): at
TTS.ConstructionAdmin.Tests.Data.Production.TTS_BootstrapperTests.CMD_createDb
()

* Database was not configured through Database method.

Thanks in advance.

Cheers,
Berryl



James Gregory

unread,
Jul 6, 2009, 3:45:16 AM7/6/09
to fluent-n...@googlegroups.com
Are you using auto-mapping or fluent mappings? The exception is an NHibernate schema validation error, which is pointing to there being an unexpected element first inside <class>. This is often the case when you haven't mapped an Id.

Can you export your hbm mappings and post them here? There's an ExportTo on the FluentMappings/AutoMappings part of the Fluently API.

Berryl Hesh

unread,
Jul 6, 2009, 12:58:19 PM7/6/09
to Fluent NHibernate
I'm using auto-mapping together with IAutoMappingOverride maps as
needed. Here are the (2) hbms for the mappings that trigger the error,
and I think from your explanation I see what the error cause is, if
not the fix just yet.

The class in the 2nd mapping, Allocation, is currently a Value Type as
opposed to an Entity; as such, my automapping doesn't generate an Id -
therefore the error. My intention is to have an Allocation table which
resolves a many-to-many relationship between Activity & StaffMember
where the natural key is the ActivityId, StaffId and dates . The
situation is like a history of pricing for a given Product at a given
RetailOutlet, where queries from both points of view are of interest.
Does this make sense?

The Allocation class itself feels more like a value type but an Entity
is starting to sound easier and I guess the alternative is some sort
of composite key. Do you have an opinion on how to do this?

Thanks!
Berryl

hbm extraacts:
--------------------------

<class name="TimeSheetActivity" table="TimeSheetActivities"
xmlns="urn:nhibernate-mapping-2.2">
<id name="Id" type="Int32" column="TimeSheetActivityID">
<generator class="identity" />
</id>
<set name="Allocations" cascade="all" access="field.camelcase-
underscore">
<key foreign-key="FK_ActivityAllocation"
column="TimeSheetActivityID" />
<one-to-many class="Allocation, ..." />
</set>
</class>

<class name="Allocation" table="Allocations" xmlns="urn:nhibernate-
mapping-2.2">
<property name="TimeSpentPeriod"
type="Data.NHibernate.UserTypes.TimePeriodUserType, ..">
<column name="PeriodStart" />
<column name="PeriodEnd" />
</property>
<many-to-one foreign-key="FK_AllocationToStaffMember" fetch="join"
not-null="true" name="StaffMember" column="StaffMemberID" />
<many-to-one foreign-key="FK_AllocationToActivity" fetch="join"
not-null="true" name="Activity" column="TimeSheetActivityID" />
</class>


On Jul 6, 12:45 am, James Gregory <jagregory....@gmail.com> wrote:
> Are you using auto-mapping or fluent mappings? The exception is an
> NHibernate schema validation error, which is pointing to there being an
> unexpected element first inside <class>. This is often the case when you
> haven't mapped an Id.
>
> Can you export your hbm mappings and post them here? There's an ExportTo on
> the FluentMappings/AutoMappings part of the Fluently API.
>

Berryl Hesh

unread,
Jul 6, 2009, 8:03:13 PM7/6/09
to Fluent NHibernate
Ok, that is certainly the fix to the error I was getting.

Evans has a brief but nice side-bar in DDD that touches on context
ultimately determining whether an object is a Value Object or an
Entity (he uses the scenario where an Address object, typically used
as an example of what a Value Object is, is really an Entity to the
post office).

I made Allocation an Entity, let the Id get auto-mapped, and all is
better. I'd be interested in any links, thoughts or experiences on
when and how NHib/FNH would typically map an associative table that
truly (from a DDD perspective and not legacy driven) does contain
value objects (composite keys?).

Again, thans for helping me understand that exception!

Cheers,
Berryl

Asbjørn Ulsberg

unread,
Jul 7, 2009, 5:22:33 PM7/7/09
to fluent-n...@googlegroups.com
Hi.

Regarding these exceptions; it would have been immensely helpful if Fluent
could catch these errors before the mappings were passed on to NHibernate,
because the exceptions NHibernate throws are often completely
unintelligible, especially for someone not intimately familiar with the
XML HBM mappings.

If there is indeed a missing Id mapping, can't Fluent throw an exception
stating that? Another way around is to wrap NHibernate's exception as an
inner exception to a FluentException that contains a bit more
human-readable descriptions on what's going on and why the exception
occurred.

These mapping exceptions complaining about missing XML elements have
bitten me many times and are pretty hard to debug, imho.


-Asbjørn

Paul Batum

unread,
Jul 12, 2009, 7:43:39 AM7/12/09
to fluent-n...@googlegroups.com
This is certainly something we would like to do. I agree that some of the exceptions that NH throws are awfully difficult to make sense of. Its simply a matter of someone doing the necessary work.


2009/7/8 Asbjørn Ulsberg <asbj...@gmail.com>

Asbjørn Ulsberg

unread,
Jul 13, 2009, 5:38:52 AM7/13/09
to fluent-n...@googlegroups.com
Yea, I just saw the thread by Mikael Henriksson regarding an unmapped
class resulting in a "KeyNotFoundException". The only way to make that
exception less understandable, would be to throw a "new Exception()". It's
quite horrible! :)

I wonder how the exception handling inside NHibernate looks when stuff
like this bubbles up and hits the consumers of the library. I haven't had
a look at the code and I'm not sure where the best place to help improve
the exception situation is.

NHibernate itself seems to be in need of more exception handling and much
better error messages, while Fluent seems to be in need of wrapping all of
NHibernate's exceptions in more intelligble Fluent-related exceptions
and/or throwing its own exceptions before the error even hits NHibernate.
I'm not sure where to do what and qite frankly, I don't feel confident
enough with NHibernate to do it myself.


-Asbjørn

On Sun, 12 Jul 2009 13:43:39 +0200, Paul Batum <paul....@gmail.com>
wrote:

James Gregory

unread,
Jul 13, 2009, 6:02:22 AM7/13/09
to fluent-n...@googlegroups.com
The problem is NH really only throws one or two exceptions, just with varying messages. For example you get a MappingException if there's anything wrong with your mapping, and you have to parse the XML to figure out what happened.

It'd be easy to provide some nice wrappers over NHs exceptions if they gave us some nicer ones to start with, but currently we'd need to parse this XML and rethrow something more useful. I'm not saying we won't do it, because it'd be really useful, it'll just be a lot of work.

2009/7/13 Asbjørn Ulsberg <asbj...@gmail.com>

Asbjørn Ulsberg

unread,
Jul 14, 2009, 1:35:43 AM7/14/09
to fluent-n...@googlegroups.com
I see.

There's still one thing I don't quite understand, though: Isn't Fluent
responsible for generating the XML that is then passed to NHibernate?
Can't Fluent, before passing the XML on to NHibernate, perform pre-checks
to see if there's anything missing or odd about the object model?

That would probably be a lot easier than trying to parse the XML and
re-throwing the exceptions from NHibernate, although having the NHibernate
exception as an inner exception would of course make debugging a bit
richer. But since NHibernate's exceptions are so extremely sparse in the
information it provides and often just bubbles exceptions thrown by
underlying failure like "KeyNotFoundException", it's not that valuable to
pass on to the consumer, really.

Thus, throwing before NHibernate even sees the XML would make it easier to
spot configuration errors and such, imho.


-Asbjørn


On Mon, 13 Jul 2009 12:02:22 +0200, James Gregory
<jagreg...@gmail.com> wrote:

> The problem is NH really only throws one or two exceptions, just with

> varying messages. For example you get a MappingException if there's *
> anything* wrong with your mapping, and you have to parse the XML to

James Gregory

unread,
Jul 14, 2009, 3:43:31 AM7/14/09
to fluent-n...@googlegroups.com
Fluent does generate xml, but the master/trunk version isn't currently capable of doing any smart inspection before it does the generation. Our Great Refactoring that is nearly complete in the model branch would be able to do this quite easily. Definitely something we should do.

2009/7/14 Asbjørn Ulsberg <asbj...@gmail.com>

Asbjørn Ulsberg

unread,
Jul 14, 2009, 12:24:22 PM7/14/09
to fluent-n...@googlegroups.com
Ok, excellent. Looking forward to seeing the fruits of The Great
Refactoring, then. :)


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

Hudson Akridge

unread,
Jul 14, 2009, 12:46:08 PM7/14/09
to fluent-n...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages