Could not compile mapping file due to DuplicateMappingException .. but I don't see any duplicate class/entity mapping

681 views
Skip to first unread message

PLen

unread,
Aug 31, 2010, 1:57:51 PM8/31/10
to nhusers
Hello,

I have two mapping files defined as:

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="FirstSample"
namespace="FirstSample.Domain" xmlns="urn:nhibernate-mapping-2.2">
<class name="RptSourceGroup" table="RPT_SOURCE_GROUP" lazy="true" >
<!--
<id name="GroupId" column="GROUP_ID" />
<id name="GroupId" type="Int64" column="GROUP_ID">
<generator class="assigned" />
</id>
-->
<id name="GroupId" type="Int64" column="GROUP_ID">
<generator class="sequence">
<param name="sequence">RPT_SOURCE_GROUP_SEQ</param>
</generator>
</id>
<property name="Name" column="NAME" />
<property name="Country" column="COUNTRY" />
<property name="Year" column="YEAR" />
<property name="Asset" column="ASSET" />
<property name="Frn" column="FRN" />
<property name="MediaType" column="MEDIA_TYPE" />
<property name="Annotation" column="ANNOTATION" />
<property name="Status" column="STATUS" />
<property name="Created" column="CREATED" />
</class>
</hibernate-mapping>

and

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="FirstSample"
namespace="FirstSample.Domain" xmlns="urn:nhibernate-mapping-2.2">
<class name="RptSource" table="RPT_SOURCE" lazy="true" >
<id name="SourceId" type="Int64" column="SOURCE_ID">
<generator class="sequence">
<param name="sequence">RPT_SOURCE_SEQ</param>
</generator>
</id>
<many-to-one name="RptSourceGroup" column="GROUP_ID" />
<property name="Name" column="NAME" />
<property name="Body" column="BODY" />
<property name="SourcePath" column="SOURCE_PATH" />
</class>
</hibernate-mapping>

They share the same namespace but have different class names. In the
code where I am configuring my session (NHibernateHelper class), I
have the following:

var configuration = new Configuration();
configuration.Configure();
configuration.AddAssembly(typeof (RptSourceGroup).Assembly);
configuration.AddAssembly(typeof (RptSource).Assembly);

When I run my app the following error is thrown during the second
AddAssembly call:

ERROR: NHibernate.MappingException: Could not compile the mapping
document: Firs
tSample.Mappings.RptSourceGroup.hbm.xml --->
NHibernate.DuplicateMappingExceptio
n: Duplicate class/entity mapping FirstSample.Domain.RptSourceGroup
at NHibernate.Cfg.Mappings.AddClass(PersistentClass
persistentClass)
at NHibernate.Cfg.XmlHbmBinding.RootClassBinder.Bind(XmlNode node,
HbmClass c
lassSchema, IDictionary`2 inheritedMetas)
at
NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddRootClasses(XmlNode
pare
ntNode, IDictionary`2 inheritedMetas)
at NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind(XmlNode
node)
at
NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument
doc)
--- End of inner exception stack trace ---
at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
at
NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument
doc)

............................................

Any clues as to why this is happening?

Thanks - Peter

Diego Mijelshon

unread,
Aug 31, 2010, 2:04:43 PM8/31/10
to nhu...@googlegroups.com
Unless your mapping files are in different assemblies, this is wrong:

configuration.AddAssembly(typeof (RptSourceGroup).Assembly);
configuration.AddAssembly(typeof (RptSource).Assembly);

 
    Diego



--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.


PLen

unread,
Aug 31, 2010, 2:59:41 PM8/31/10
to nhusers
Diego,

Thanks for the response. This is where I am confused. I am new to
NHibernate so I got started based on examples I found which seemed to
work. For each table (entity), I have a different hbm.xml mapping
file. The mapping files (two) live in the FirstSample.Mappings
directory (FirstSample is the C# project). The two entity class files
live in the FirstSample.Domain directory and as such each of the
classes are in the namespace "FirstSample.Domain". So while they are
two different classes, both of the mapping files have this same entry:

<hibernate-mapping assembly="FirstSample"
namespace="FirstSample.Domain" xmlns="urn:nhibernate-mapping-2.2">

So when I add the assemblies to the configuration, the two lines:

configuration.AddAssembly(typeof (RptSourceGroup).Assembly);
configuration.AddAssembly(typeof (RptSource).Assembly);

really evaluate to:

configuration.AddAssembly(FirstSample.Domain.RptSourceGroup.Assembly);
configuration.AddAssembly(FirstSample.Domain.RptSource.Assembly);

Now, I am not sure what that really means, but apparently it is
causing a problem which I don't understand. My understanding was that
the assembly name is the name where the domain classes are
implemented, which looked to be the project name. Is this not
correct? I get other errors if I give the files an assembly name of
somethiong random (ex. MyAssemblyName1 and MyAssemblyName2). Are you
saying that because they are in the same assembly (I take that to mean
the same project), that both class definitions need to be within the
same file and not individual files?

Any help on understanding this would be very much appreciated.

Thanks - Peter

MattO

unread,
Aug 31, 2010, 3:04:17 PM8/31/10
to nhusers
You only call AddAssembly once, from there nHibernate takes care of
the rest.

here's an example: http://www.martinwilley.com/net/code/nhibernate/sessionmanager.html

Check out the s#arp sources if you want some prebuilt code that does
all this stuff for you.

I don't even use AddAssembly in my code as everything is in one DLL

PLen

unread,
Aug 31, 2010, 3:34:50 PM8/31/10
to nhusers

MattO,

So it looks like all that is requireed is to load one assembly into
the configuration. The API doc states that the AddAssembly call "Adds
all the assembly's embedded resources whose names end
with .hbm.xml.". When the one is added, all of the mapping files will
be picked up. At least that is how I interpret that. In a test to
see what would happen if I did not add an assembly, I got the error
"No persister for: FirstSample.Domain.RptSourceGroup".

Does that sound about right?

Thanks - Peter

Diego Mijelshon

unread,
Aug 31, 2010, 3:48:11 PM8/31/10
to nhu...@googlegroups.com
If both classes are in the same assembly, you are adding that assembly's mappings twice.
 
    Diego



Thanks - Peter

Reply all
Reply to author
Forward
0 new messages