An entity with multiple Many-To-Many lists of the same type?

19 views
Skip to first unread message

Rob Gibbens

unread,
Dec 16, 2009, 11:37:01 AM12/16/09
to nhusers
Does anybody know how I would map an entity with two many-to-many
collections of the same child type.

My database structure is this....
-----------------------------------------------------------------
The "normal" relationship will be....

tbl_Parent
col_Parent_ID

tbl_Parent_Child_Xref
col_Parent_ID
col_Child_ID

tbl_Child
col_Child_ID

-----------------------------------------------------------------
The alternative relationship is...

tbl_Parent
col_Parent_ID

tbl_Include_ParentChild_Xref
col_Parent_ID
col_Child_ID

tbl_Child
col_Child_ID

-----------------------------------------------------------------
The entity and mapping look like this...

public partial class ParentEntity : AuditableDataEntity<ParentEntity>
{
public virtual IList<ChildEntity> Children { get; set; }
public virtual IList<ChildEntity> IncludedChildren { get; set; }
}

public partial class ParentMap : IAutoMappingOverride<ParentEntity>
{
public void Override(AutoMapping<ParentEntity> mapping)
{
mapping.Table("tbl_Parent");

mapping.HasManyToMany(x => x.Children)
.Table("tbl_Parent_Child_Xref")
.ParentKeyColumn("col_Parent_ID")
.ChildKeyColumn("col_Child_ID")
.Inverse()
.Cascade.All();

mapping.HasManyToMany(x => x.IncludedChildren)
.Table("tbl_Include_ParentChild_Xref")
.ParentKeyColumn("col_Parent_ID")
.ChildKeyColumn("col_Child_ID")
.Inverse()
.Cascade.All();
}
}
-----------------------------------------------------------------


The error that I'm getting is
"System.NotSupportedException: Can't figure out what the other side of
the many-to-many property 'Children' should be."

I'm using NHibernate 2.1.2, FluentNhibernate 1.0.

Diego Mijelshon

unread,
Dec 16, 2009, 12:59:59 PM12/16/09
to nhusers
Well, NHibernate supports it without a problem, so it must be a problem with that fluent mapping.
What's the code for ChildEntity? I see you have Inverse() on the mapping, so there must be a related, non-inverse mapping on the other side...

Here's my hbm mapping for that:

 <class name="ParentEntity" table="tbl_Parent"> <id ...> <bag name="Children" table="tbl_Parent_Child_Xref"> <key column="col_Parent_ID"/> <many-to-many class="ChildEntity" column="col_Child_ID"/> </bag> <bag name="IncludedChildren" table="tbl_Include_ParentChild_Xref"> <key column="col_Parent_ID"/> <many-to-many class="ChildEntity" column="col_Child_ID"/> </bag> </class>


   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.



Rob Gibbens

unread,
Dec 16, 2009, 3:09:47 PM12/16/09
to nhusers
Thanks for the quick reply.

The child entity and mapping look like this (just the opposite of the
parent)...

------------------------------------------------------------------------------------------------------
public partial class ChildEntity : AuditableDataEntity<ChildEntity>
{
public virtual IList<ParentEntity> Parents { get; set; }
public virtual IList<ParentEntity> IncludedParents { get; set; }
}

public partial class ChildMap : IAutoMappingOverride<ChildEntity>
{
public void Override(AutoMapping<ChildEntity> mapping)
{
mapping.Table("tbl_Child");

mapping.HasManyToMany(x => x.Parents)
.Table("tbl_Parent_Child_Xref")
.ParentKeyColumn("col_Child_ID")
.ChildKeyColumn("col_Parent_ID")
.Inverse()
.Cascade.All();

mapping.HasManyToMany(x => x.IncludedParents)
.Table("tbl_Include_ParentChild_Xref")
.ParentKeyColumn("col_Child_ID")
.ChildKeyColumn("col_Parent_ID")
.Inverse()
.Cascade.All();
}
}

----------------------------------------------------------------------------------------
> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com >
> > .

Rob Gibbens

unread,
Dec 16, 2009, 3:15:37 PM12/16/09
to nhusers
Based on your advice, I also remove the .Inverse from the child's
mapping to the parent, but I still get the same problem.

Diego Mijelshon

unread,
Dec 16, 2009, 3:19:23 PM12/16/09
to nhusers
Can you send the full exception stack trace?

   Diego


To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.

Rob Gibbens

unread,
Dec 16, 2009, 3:29:41 PM12/16/09
to nhusers
------ Test started: Assembly: MyCompany.Data.Testing.dll ------

TestCase
'MyCompany.Data.Testing.OffersTest.GetOffersWithIncludedApplications'
failed: TestFixtureSetUp failed in OffersTest
TestFixture failed:
Castle.MicroKernel.ComponentActivator.ComponentActivatorException :
ComponentActivator: could not instantiate MyCompany.Data.NHFactory
----> System.Reflection.TargetInvocationException : Exception has
been thrown by the target of an invocation.
----> System.NotSupportedException : Can't figure out what the other
side of the many-to-many property 'Applications' should be.
at
Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstance
(CreationContext context, Object[] arguments, Type[] signature)
at
Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.Instantiate
(CreationContext context)
at
Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate
(CreationContext context)
at
Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create
(CreationContext context)
at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.Resolve
(CreationContext context)
at Castle.MicroKernel.Lifestyle.SingletonLifestyleManager.Resolve
(CreationContext context)
at Castle.MicroKernel.Handlers.DefaultHandler.Resolve
(CreationContext context)
at
Castle.MicroKernel.Resolvers.DefaultDependencyResolver.ResolveServiceDependency
(CreationContext context, ComponentModel model, DependencyModel
dependency)
at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.Resolve
(CreationContext context, ISubDependencyResolver
contextHandlerResolver, ComponentModel model, DependencyModel
dependency)
at
Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateConstructorArguments
(ConstructorCandidate constructor, CreationContext context, Type[]&
signature)
at
Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.Instantiate
(CreationContext context)
at
Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate
(CreationContext context)
at
Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create
(CreationContext context)
at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.Resolve
(CreationContext context)
at Castle.MicroKernel.Handlers.DefaultHandler.Resolve
(CreationContext context)
at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler
handler, Type service, IDictionary additionalArguments)
at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler
handler, Type service)
at Castle.MicroKernel.DefaultKernel.get_Item(Type service)
at Castle.Windsor.WindsorContainer.Resolve(Type service)
at Castle.Windsor.WindsorContainer.get_Item(Type service)
at MyCompany.Toolkit.Reflection.ObjectFactory.Create[T](Boolean
isErrorThrown) in C:\Development\dev\Toolkit\Toolkit.Shared\Reflection
\ObjectFactory.cs:line 124
at MyCompany.Toolkit.Reflection.ObjectFactory.Create[T]() in C:
\Development\dev\Toolkit\Toolkit.Shared\Reflection
\ObjectFactory.cs:line 117
at MyCompany.Toolkit.Testing.Utilities.DataTestCase..ctor() in C:
\Development\dev\Toolkit\Toolkit.Testing\Utilities\TestCase.cs:line 92
at MyCompany.Data.Testing.OffersTest..ctor() in C:\Development\dev
\Data\Data.Testing\OffersTest.cs:line 30
--TargetInvocationException
at System.RuntimeMethodHandle._InvokeMethodFast(Object target,
Object[] arguments, SignatureStruct& sig, MethodAttributes
methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object
[] arguments, Signature sig, MethodAttributes methodAttributes,
RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeConstructorInfo.Invoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters,
CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[]
parameters)
at
Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.FastCreateInstance
(Type implType, Object[] arguments, Type[] signature)
at
Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstance
(CreationContext context, Object[] arguments, Type[] signature)
--NotSupportedException
at
FluentNHibernate.MappingModel.BiDirectionalManyToManyPairingVisitor.ProcessCollection
(ICollectionMapping mapping)
at
FluentNHibernate.MappingModel.DefaultMappingModelVisitor.ProcessBag
(BagMapping bagMapping)
at
FluentNHibernate.MappingModel.Collections.BagMapping.AcceptVisitor
(IMappingModelVisitor visitor)
at FluentNHibernate.MappingModel.DefaultMappingModelVisitor.Visit
(ICollectionMapping collectionMapping)
at FluentNHibernate.MappingModel.MappedMembers.AcceptVisitor
(IMappingModelVisitor visitor)
at
FluentNHibernate.MappingModel.ClassBased.ClassMappingBase.AcceptVisitor
(IMappingModelVisitor visitor)
at
FluentNHibernate.MappingModel.ClassBased.ClassMapping.AcceptVisitor
(IMappingModelVisitor visitor)
at FluentNHibernate.MappingModel.DefaultMappingModelVisitor.Visit
(ClassMapping classMapping)
at FluentNHibernate.MappingModel.HibernateMapping.AcceptVisitor
(IMappingModelVisitor visitor)
at FluentNHibernate.PersistenceModel.ApplyVisitors(IEnumerable`1
mappings)
at FluentNHibernate.PersistenceModel.BuildMappings()
at FluentNHibernate.PersistenceModel.EnsureMappingsBuilt()
at FluentNHibernate.PersistenceModel.Configure(Configuration cfg)
at FluentNHibernate.Automapping.AutoPersistenceModel.Configure
(Configuration configuration)
at MyCompany.Data.NHFactory..ctor(String key, String configuration,
String mappings, Boolean isProfiled, Boolean isexported) in C:
\Development\dev\Data\Data.NHibernate\NHFactory.cs:line 154
at MyCompany.Data.NHFactory..ctor(String key, String configuration,
Boolean isProfiled) in C:\Development\dev\Data\Data.NHibernate
\NHFactory.cs:line 39
DEBUG [NHFactory:oracle] - Profiler initialized
DEBUG [NHFactory:oracle] - Configuration initialized

0 passed, 1 failed, 0 skipped, took 3.34 seconds (NUnit 2.5.2).
> > <nhusers%2Bunsu...@googlegroups.com<nhusers%252Bunsubscribe@googlegroup s.com>>

Rob Gibbens

unread,
Dec 16, 2009, 3:39:08 PM12/16/09
to nhusers
Also, I found this post, but I'm not sure if the fix was ever put in
place.

http://groups.google.com/group/fluent-nhibernate/browse_thread/thread/4612913df1a4d916/9b07e19ce9e0de9e?lnk=raot
> > > > > >   col_Child_ID...
>
> read more »

Diego Mijelshon

unread,
Dec 16, 2009, 4:04:01 PM12/16/09
to nhusers
I copied your code and it worked flawlessly.
Is your fluentnhibernate.dll up to date?

   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.

Rob Gibbens

unread,
Dec 16, 2009, 4:24:32 PM12/16/09
to nhusers
I think it's the trunk from no more than 3 weeks ago. I'll update and
try again.

On Dec 16, 4:04 pm, Diego Mijelshon <di...@mijelshon.com.ar> wrote:
> I copied your code and it worked flawlessly.
> Is your fluentnhibernate.dll up to date?
>
>    Diego
>
>
>
> On Wed, Dec 16, 2009 at 17:39, Rob Gibbens <robgibb...@gmail.com> wrote:
> > Also, I found this post, but I'm not sure if the fix was ever put in
> > place.
>
> >http://groups.google.com/group/fluent-nhibernate/browse_thread/thread...
> > > > > > > class="ChildEntity" column="col_Child_ID"/>...
>
> read more »

Rob Gibbens

unread,
Dec 16, 2009, 4:57:43 PM12/16/09
to nhusers
Ok, I updated to the latest trunk of FNH, and am getting the same
error. Here is the exact code that I'm trying to run (with real
names, but with extra properties taken out)....


System.NotSupportedException: Can't figure out what the other side of
the many-to-many property 'Applications' should be.

/
**********************************************************************************/

namespace MyCompany.Data.Entities
{
using System.Collections.Generic;
using MyCompany.Toolkit.Data;

public class ApplicationEntity :
AuditableDataEntity<ApplicationEntity>
{
public ApplicationEntity()
{
this.Offers = new List<OfferEntity>();
this.IncludedOffers = new List<OfferEntity>();
}

public virtual IList<OfferEntity> Offers { get; set; }
public virtual IList<OfferEntity> IncludedOffers { get; set; }
}
}

/
**********************************************************************************/

namespace MyCompany.Data.Mappings
{
using MyCompany.Data.Enums;
using FluentNHibernate.Automapping;
using FluentNHibernate.Automapping.Alterations;
using MyCompany.Data.Entities;

public partial class ApplicationMap :
IAutoMappingOverride<ApplicationEntity>
{
public void Override(AutoMapping<ApplicationEntity> mapping)
{
mapping.Table("APPLICATION");

mapping.Id(x => x.Id, "APPLICATION_ID", "APPLICATION_SQ");

/****************************************************/
mapping.HasManyToMany(x => x.Offers)
.Table("APPLICATION_OFFER_XREF")
.ParentKeyColumn("APPLICATION_ID")
.ChildKeyColumn("OFFER_ID")
.Inverse()
.Cascade.All();

mapping.HasManyToMany(x => x.IncludedOffers)
.Table("APPLICATION_OFFER_XREF")
.ParentKeyColumn("APPLICATION_ID")
.ChildKeyColumn("OFFER_ID")
.Inverse()
.Cascade.All();
}
}
}

/
**********************************************************************************/

namespace MyCompany.Data.Entities
{
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System;
using System.Collections.Generic;
using MyCompany.Data.Enums;
using MyCompany.Toolkit.Data;

public partial class OfferEntity : AuditableDataEntity<OfferEntity>
{
public OfferEntity()
{
this.Applications = new List<ApplicationEntity>();
this.IncludedApplications = new List<ApplicationEntity>();
}

public virtual IList<ApplicationEntity> Applications { get; set; }
public virtual IList<ApplicationEntity> IncludedApplications
{ get; set;}
}
}

/
**********************************************************************************/

namespace MyCompany.Data.Mappings
{
using FluentNHibernate.Automapping;
using FluentNHibernate.Automapping.Alterations;
using MyCompany.Data.Entities;
using MyCompany.Data.Enums;
using MyCompany.Data;

public partial class OfferMap : IAutoMappingOverride<OfferEntity>
{
public void Override(AutoMapping<OfferEntity> mapping)
{
mapping.Table("OFFER");

mapping.Id(x => x.Id, "OFFER_ID", "OFFER_SQ");

/****************************************************/
mapping.HasManyToMany(x => x.Applications)
.Table("APPLICATION_OFFER_XREF")
.ParentKeyColumn("OFFER_ID")
.ChildKeyColumn("APPLICATION_ID")
.Inverse()
.Cascade.All();

mapping.HasManyToMany(x => x.IncludedApplications)
.Table("APP_INCLUDE_OFFER_XREF")
.ParentKeyColumn("OFFER_ID")
.ChildKeyColumn("APPLICATION_ID")
.Inverse()
.Cascade.All();

/****************************************************/
> > > wrote:...
>
> read more »
Reply all
Reply to author
Forward
0 new messages