Glad you got it working, but you really shouldn't need that UserSponsor
class at all. The whole point of ManyToMany mappings is to remove the need
for joining classes.
One thing I got wrong with the mapping I last sent you was that I did not
specify which side of the relation is the inverse side. So the mapping
should have been:

   HasManyToMany<User>(x => x.Sponsors)
               .Table("UserSponsor")
               .ParentKeyColumn("UserId")
               .ChildKeyColumn("SponsorId")
               .Inverse()
               .AsBag();

   HasManyToMany<User>(x => x.SponsoredUsers)
               .Table("UserSponsor")
               .ParentKeyColumn("SponsorId")
               .ChildKeyColumn("UserId")
               .AsBag();



On Fri, Jan 15, 2010 at 4:44 AM, TheNephalim <[email protected]>wrote:

> I feel completely stupid now.  I changed the type in the User class
> from User to UserSponsor.  When I run it, I get a list that includes
> the User information and the Sponsor information.
>
> Thanks again for your help.
>
> -Robert Eberhart
>
> On Jan 14, 12:16 pm, TheNephalim <[email protected]> wrote:
> > Thanks for your response.  Unfortunately, though, that method did not
> > work.
> >
> > That being said...I did make some progress.
> >
> > I kept the UserSponsor class and the UserSponsorMap as indicated in
> > one of my previous posts.
> >
> > I added the following to the UserMap class:
> >
> >             HasMany<UserSponsor>(x => x.Sponsors)
> >                 .Table("UserSponsor")
> >                 .KeyColumn("UserId")
> >                 .Inverse()
> >                 .AsBag();
> >             HasMany<UserSponsor>(x => x.SponsoredUsers)
> >                 .Table("UserSponsor")
> >                 .KeyColumn("SponsorId")
> >                 .Inverse()
> >                 .AsBag();
> >
> > Everything compiles and runs, when I access the either the Sponsors or
> > the SponsoredUsers objects, I receive the error "illegal access to
> > loading collection."  I checked the log produced and found the
> > following the text included below.  The SQL being produced is correct,
> > but the actual error is "The value "Peotsc.Entities.UserSponsor" is
> > not of type "Peotsc.Entities.User" and cannot be used in this generic
> > collection."
> >
> > So, I've moved one step closer...but still not quite there yet. :-)
> >
> > -Robert Eberhart
> >
> > could not initialize a collection:
> > [Peotsc.Entities.User.Sponsors#3061]
> > [ SELECT sponsors0_.[UserId] as UserId2_2_, sponsors0_.[UserSponsorId]
> > as UserSpon1_2_, sponsors0_.[UserSponsorId] as UserSpon1_9_1_,
> > sponsors0_.[UserId] as UserId2_9_1_, sponsors0_.[SponsorId] as
> > SponsorId3_9_1_, user1_.[UserId] as UserId1_0_0_, user1_.
> > [UserFirstName] as UserFirs2_0_0_, user1_.[UserMiddleName] as
> > UserMidd3_0_0_, user1_.[UserLastName] as UserLast4_0_0_, user1_.
> > [UserName] as UserName5_0_0_, user1_.[UserApprovedYn] as
> > UserAppr6_0_0_, user1_.[UserLoginEnabledYn] as UserLogi7_0_0_, user1_.
> > [UserTitle] as UserTitle8_0_0_, user1_.[UserEmail] as UserEmail9_0_0_,
> > user1_.[ConfirmationNumber] as Confirm10_0_0_, user1_.
> > [ApplicationDate] as Applica11_0_0_, user1_1_.[UserBldgRoom] as
> > UserBldg2_1_0_, user1_1_.[UserBldgId] as UserBldgId3_1_0_, user1_1_.
> > [UserGradeId] as UserGrad4_1_0_, case when user1_1_.[UserId] is not
> > null then 1 when user1_2_.[UserId] is not null then 2 when user1_.
> > [UserId] is not null then 0 end as clazz_0_ FROM UserSponsor
> > sponsors0_ left outer join SystemUser user1_ on sponsors0_.[SponsorId]
> > =user1_.[UserId] left outer join GovernmentUser user1_1_ on user1_.
> > [UserId]=user1_1_.[UserId] left outer join NonGovernmentUser user1_2_
> > on user1_.[UserId]=user1_2_.[UserId] WHERE sponsors0_.[useri...@p0 ]
> >
> > System.ArgumentException: The value "Peotsc.Entities.UserSponsor" is
> > not of type "Peotsc.Entities.User" and cannot be used in this generic
> > collection.
> > Parameter name: value
> >    at System.ThrowHelper.ThrowWrongValueTypeArgumentException(Object
> > value, Type targetType)
> >    at System.Collections.Generic.List`1.VerifyValueType(Object value)
> >    at System.Collections.Generic.List`1.System.Collections.IList.Add
> > (Object item)
> >    at NHibernate.Collection.PersistentBag.ReadFrom(IDataReader reader,
> > ICollectionPersister role, ICollectionAliases descriptor, Object
> > owner)
> >    at NHibernate.Loader.Loader.ReadCollectionElement(Object
> > optionalOwner, Object optionalKey, ICollectionPersister persister,
> > ICollectionAliases descriptor, IDataReader rs, ISessionImplementor
> > session)
> >    at NHibernate.Loader.Loader.ReadCollectionElements(Object[] row,
> > IDataReader resultSet, ISessionImplementor session)
> >    at NHibernate.Loader.Loader.GetRowFromResultSet(IDataReader
> > resultSet, ISessionImplementor session, QueryParameters
> > queryParameters, LockMode[] lockModeArray, EntityKey
> > optionalObjectKey, IList hydratedObjects, EntityKey[] keys, Boolean
> > returnProxies)
> >    at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session,
> > QueryParameters queryParameters, Boolean returnProxies)
> >    at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections
> > (ISessionImplementor session, QueryParameters queryParameters, Boolean
> > returnProxies)
> >    at NHibernate.Loader.Loader.LoadCollection(ISessionImplementor
> > session, Object id, IType type)
> > 2010-01-14 12:09:46,263 [8] WARN  - System.ArgumentException: The
> > value "Peotsc.Entities.UserSponsor" is not of type
> > "Peotsc.Entities.User" and cannot be used in this generic collection.
> > Parameter name: value
> >    at System.ThrowHelper.ThrowWrongValueTypeArgumentException(Object
> > value, Type targetType)
> >    at System.Collections.Generic.List`1.VerifyValueType(Object value)
> >    at System.Collections.Generic.List`1.System.Collections.IList.Add
> > (Object item)
> >    at NHibernate.Collection.PersistentBag.ReadFrom(IDataReader reader,
> > ICollectionPersister role, ICollectionAliases descriptor, Object
> > owner)
> >    at NHibernate.Loader.Loader.ReadCollectionElement(Object
> > optionalOwner, Object optionalKey, ICollectionPersister persister,
> > ICollectionAliases descriptor, IDataReader rs, ISessionImplementor
> > session)
> >    at NHibernate.Loader.Loader.ReadCollectionElements(Object[] row,
> > IDataReader resultSet, ISessionImplementor session)
> >    at NHibernate.Loader.Loader.GetRowFromResultSet(IDataReader
> > resultSet, ISessionImplementor session, QueryParameters
> > queryParameters, LockMode[] lockModeArray, EntityKey
> > optionalObjectKey, IList hydratedObjects, EntityKey[] keys, Boolean
> > returnProxies)
> >    at NHibernate.Loader.Loader.DoQuery(ISessionImplementor session,
> > QueryParameters queryParameters, Boolean returnProxies)
> >    at NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections
> > (ISessionImplementor session, QueryParameters queryParameters, Boolean
> > returnProxies)
> >    at NHibernate.Loader.Loader.LoadCollection(ISessionImplementor
> > session, Object id, IType type)
> > 2010-01-14 12:09:46,263 [8] ERROR - The value
> > "Peotsc.Entities.UserSponsor" is not of type "Peotsc.Entities.User"
> > and cannot be used in this generic collection.
> > Parameter name: value
> > 2010-01-14 12:09:46,732 [8] ERROR - illegal access to loading
> > collection
> > NHibernate.LazyInitializationException: illegal access to loading
> > collection
> > 2010-01-14 12:09:46,732 [8] ERROR - illegal access to loading
> > collection
> > NHibernate.LazyInitializationException: illegal access to loading
> > collection
> > 2010-01-14 12:09:50,435 [8] ERROR - illegal access to loading
> > collection
> > NHibernate.LazyInitializationException: illegal access to loading
> > collection
> > 2010-01-14 12:09:51,638 [8] ERROR - illegal access to loading
> > collection
> > NHibernate.LazyInitializationException: illegal access to loading
> > collection
> >
> > On Jan 13, 4:52 pm, Paul Batum <[email protected]> wrote:
> >
> > > Robert,
> >
> > > It does make things more straightforward if you add a PK to the joining
> > > table rather than using a composite key. However you do NOT need to
> create a
> > > class for the joining type, nor do you need a map. So you don't need a
> > > UserSponsor class, and you don't need a UserSponsorMap. What is
> necessary,
> > > which I forgot to mention, is to make sure that the relationship has
> > > properties for both sides. So your user class needs to look something
> like
> > > this:
> >
> > > public class User
> > > {
> > > public virtual IList<User> Sponsors;
> > > public virtual IList<User> SponsoredUsers;
> >
> > > }
> >
> > > and then the map should look roughly like this (i think!):
> >
> > >    HasManyToMany<User>(x => x.Sponsors)
> > >                .Table("UserSponsor")
> > >                .ParentKeyColumn("UserId")
> > >                .ChildKeyColumn("SponsorId")
> > >                .AsBag();
> >
> > >    HasManyToMany<User>(x => x.SponsoredUsers)
> > >                .Table("UserSponsor")
> > >                .ParentKeyColumn("SponsorId")
> > >                .ChildKeyColumn("UserId")
> > >                .AsBag();
> >
> > > Now FNH shouldn't complain about not being able to find the other side.
> That
> > > mapped SponsoredUsers property is the other side.
> >
> > > You'll have to make sure that the PK on the UserSponsor table is what
> FNH
> > > expects it to be, or to explicitly specify it in the mapping above.
> >
> > > Let me know how it goes.
> >
> > > Paul.
> >
> > > On Thu, Jan 14, 2010 at 12:57 AM, TheNephalim <
> [email protected]>wrote:
> >
> > > > Mr. Gregory:
> >
> > > > I just wanted to say that I love Fluent NHibernate.  Thanks for all
> of
> > > > your hard work.  :-)
> >
> > > > -Robert Eberhart
> >
> > > > On Jan 13, 8:48 am, James Gregory <[email protected]> wrote:
> > > > > If you're getting that particular exception, then there's an issue
> I need
> > > > to
> > > > > fix. That exception gets thrown when FNH can't find the other side
> of a
> > > > > relationship, usually due to there being some ambiguity in the
> naming of
> > > > > collections. Sounds like we either need to be smarter, or stop
> throwing
> > > > that
> > > > > exception.
> >
> > > > > On Wed, Jan 13, 2010 at 1:42 PM, TheNephalim <
> [email protected]
> > > > >wrote:
> >
> > > > > > Mr. Batum,
> >
> > > > > > Thanks for taking the time to respond to my post.  :-)
> >
> > > > > > I thought the same thing as you did, and has actually implemented
> the
> > > > > > same code that you posted.  However, I kept getting an error
> every
> > > > > > time I built the session with FNH stating that it "Can't figure
> out
> > > > > > what the other side of the many-to-many property 'Sponsors'
> should
> > > > > > be."
> >
> > > > > > After doing some research, I happened upon an article here:
> >
> > > > > >
> http://kyle.baley.org/ManytomanyRelationshipsWithDataAttachedInNHiber.
> > > > ..
> >
> > > > > > The result was that I added a identity field to the UserSponsor
> table
> > > > > > (for simplicity's sake; I'm aware that you can make use
> CompositeKey
> > > > > > ()).  I made UserSponsor an entity and mapped it.  The code is
> > > > > > included below for both the entity and the mapping.  If I get a
> list
> > > > > > for either the User or the Sponsor side, I can get results, the
> issue
> > > > > > I then have is how to access this entity in the UserMap, which is
> > > > > > causing me angst as well.
> >
> > > > > > If you or anyone else has further insights on this, it would be
> > > > > > greatly appreciated.  This is my first foray into FNH and it
> appears
> > > > > > that I threw myself into the deep end of the pool.  :-)
> >
> > > > > > Code for UserSponsor class:
> >
> > > > > >    public class UserSponsor {
> > > > > >        private int _id;
> > > > > >        private User _user;
> >
> > ...
> >
> > read more ยป
>
> --
> You received this message because you are subscribed to the Google Groups
> "Fluent NHibernate" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<fluent-nhibernate%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/fluent-nhibernate?hl=en.
>
>
>
>
--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.

Reply via email to