I can think about 2 options:

1.
<class name="RelatedBlog">
 <composite-id>
    <key-many-to-one name="OriginalBlog">
     <column name="OriginalUserId" />
     <column name="OriginalBlogId" />
  </many-to-one>
 <key-many-to-one name="OtherBlog">
     <column name="OtherUserId" />
     <column name="OtherBlogId" />
  </many-to-one>
   <key-property name="Id" />
 </composite-id>
</class>

2.
<class name="RelatedBlog">
 <composite-id>
   <key-many-to-one name="User" column="UserId" />
   <key-property name="Id" />
 </composite-id>
 <many-to-one name="OriginalBlog">
     <column name="UserId" insert="false" update="false"/>
     <column name="OriginalBlogId" />
  </many-to-one>
 <many-to-one name="OtherBlog">
     <column name="UserId" insert="false" update="false" />
     <column name="OtherBlogId" />
  </many-to-one>
</class>

2012/1/1 Bill Pierce <[email protected]>

> Thank you Itzik, that solved the issue.  Not sure why I didn't see
> that.  Was working too late I guess.  With that solved, a related
> issue arises.  Suppose I have an additional entity, RelatedBlog, with
> mapping like:
>
> <class name="RelatedBlog">
>   <composite-id>
>    <key-many-to-one name="User" column="UserId" />
>    <key-property name="Id" />
>  </composite-id>
>   <many-to-one name="OriginalBlog">
>      <column name="UserId" />
>      <column name="OriginalBlogId" />
>   </many-to-one>
>  <many-to-one name="OtherBlog">
>      <column name="UserId" />
>      <column name="OtherBlogId" />
>   </many-to-one>
> </class>
>
> And I add the following to the Blog mapping:
>
> <bag name="RelatedBlogs" inverse="true">
>     <key>
>      <column name="UserId" />
>       <column name="OriginalBlogId" />
>    </key>
>    <one-to-many class="RelatedBlog" />
> </bag>
>
> The same issue arrises in the RelatedBlog entity.  Any additional
> ideas?
>
>
> On Dec 31, 9:57 am, Itzik Saban <[email protected]> wrote:
> > Seems to me you should change the Post's mapping, after all, it's the one
> > how's causing the problem.
> > to my opinion it should be like this:
> >
> > <class name="Post">
> >   <composite-id>
> >     <key-many-to-one name="Blog">
> >           <column name="UserId" />
> >            <column name="BlogId" not-null="true" />
> >     </key-many-to-one>
> >     <key-property name="Id" />
> >   </composite-id>
> >   <property name="Content" not-null="true" />
> > </class>
> >
> > try it.
> >
> > 2011/12/31 Bill Pierce <[email protected]>
> >
> >
> >
> >
> >
> > > I know Composite-Id is the devil, but assume you were working on a
> legacy
> > > DB.  Or rather, assume you were working on a brand new database
> technology (
> > >http://msdn.microsoft.com/en-us/library/windowsazure/hh597469.aspx)
> that
> > > requires that all Ids be composite.
> >
> > > Using NHibernate 3.2 GA and given the mapping below:
> > > <class name="User">
> > >   <id name="Id" />
> > >   <property name="Email" not-null="true" />
> > > </class>
> >
> > > <class name="Blog">
> > >   <composite-id>
> > >     <key-many-to-one name="User" column="UserId" />
> > >     <key-property name="Id" />
> > >   </composite-id>
> > >   <property name="Title" not-null="true" />
> > >   <bag name="Posts" inverse="true" cascade="all-delete-orphan">
> > >     <key>
> > >       <column name="UserId" />
> > >       <column name="BlogId" />
> > >     </key>
> > >     <one-to-many class="Post" />
> > >   </bag>
> > > </class>
> >
> > > <class name="Post">
> > >   <composite-id>
> > >     <key-many-to-one name="User" column="UserId" />
> > >     <key-property name="Id" />
> > >   </composite-id>
> > >   <property name="Content" not-null="true" />
> > >   <many-to-one name="Blog">
> > >     <column name="UserId" />
> > >     <column name="BlogId" not-null="true" />
> > >   </many-to-one>
> > > </class>
> >
> > > This code:
> > > using (ISession session = OpenSession())
> > > {
> > >     using (ITransaction transaction = session.BeginTransaction())
> > >     {
> > >         User user = new User { Id = 111, Email = "[email protected]" };
> > >         session.Save(user);
> > >         Blog blog = new Blog { User = user, Id = 222, Title = "Foo" };
> > >         Post post = new Post { User = user, Id = 333, Blog = blog,
> Content
> > > = "Bar" };
> > >         blog.Posts.Add(post);
> > >         session.Save(blog);
> > >         transaction.Commit();
> > >     }
> > > }
> >
> > > Produces this log snippet (full log attached):
> > > Building an IDbCommand object for the SqlString: INSERT INTO Post
> > > (Content, BlogId, UserId, Id) VALUES (?, ?, ?, ?)
> > > Dehydrating entity: [Post]
> > > binding 'Bar' to parameter: 0
> > > binding '111' to parameter: 1
> > > binding '222' to parameter: 2
> > > binding '111' to parameter: 3
> > > binding '333' to parameter: 4
> >
> > > And fails with this exception:
> > > System.IndexOutOfRangeException: Invalid index 4 for this
> > > SqlParameterCollection with Count=4.
> > > at System.Data.SqlClient.SqlParameterCollection.RangeCheck(Int32 index)
> > > at System.Data.SqlClient.SqlParameterCollection.GetParameter(Int32
> index)
> > > at
> > >
> System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item(­Int32
> > > index)
> > > at NHibernate.Type.GuidType.Set(IDbCommand cmd, Object value, Int32
> index)
> > > in d:\CSharp\NH\NH\nhibernate\src\NHibernate\Type\GuidType.cs: line 59
> > > at NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object
> value,
> > > Int32 index) in
> > > d:\CSharp\NH\NH\nhibernate\src\NHibernate\Type\NullableType.cs: line
> 180
> > > at NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object
> value,
> > > Int32 index, ISessionImplementor session) in
> > > d:\CSharp\NH\NH\nhibernate\src\NHibernate\Type\NullableType.cs: line
> 139
> > > at NHibernate.Type.ComponentType.NullSafeSet(IDbCommand st, Object
> value,
> > > Int32 begin, ISessionImplementor session) in
> > > d:\CSharp\NH\NH\nhibernate\src\NHibernate\Type\ComponentType.cs: line
> 221
> > > at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object
> > > id, Object[] fields, Object rowId, Boolean[] includeProperty,
> Boolean[][]
> > > includeColumns, Int32 table, IDbCommand statement, ISessionImplementor
> > > session, Int32 index) in
> > >
> d:\CSharp\NH\NH\nhibernate\src\NHibernate\Persister\Entity\AbstractEntityPe­rsister.cs:
> > > line 2421
> > > at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object
> id,
> > > Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object
> > > obj, ISessionImplementor session) in
> > >
> d:\CSharp\NH\NH\nhibernate\src\NHibernate\Persister\Entity\AbstractEntityPe­rsister.cs:
> > > line 2649
> > > at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object
> id,
> > > Object[] fields, Object obj, ISessionImplementor session) in
> > >
> d:\CSharp\NH\NH\nhibernate\src\NHibernate\Persister\Entity\AbstractEntityPe­rsister.cs:
> > > line 3055
> > > at NHibernate.Action.EntityInsertAction.Execute() in
> > > d:\CSharp\NH\NH\nhibernate\src\NHibernate\Action\EntityInsertAction.cs:
> > > line 59
> > > at NHibernate.Engine.ActionQueue.Execute(IExecutable executable) in
> > > d:\CSharp\NH\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs: line
> 136
> > > at NHibernate.Engine.ActionQueue.ExecuteActions(IList list) in
> > > d:\CSharp\NH\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs: line
> 126
> > > at NHibernate.Engine.ActionQueue.ExecuteActions() in
> > > d:\CSharp\NH\NH\nhibernate\src\NHibernate\Engine\ActionQueue.cs: line
> 169
> > > at
> > >
> NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IE­ventSource
> > > session) in
> > >
> d:\CSharp\NH\NH\nhibernate\src\NHibernate\Event\Default\AbstractFlushingEve­ntListener.cs:
> > > line 241
> > > at
> NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent
> > > event) in
> > >
> d:\CSharp\NH\NH\nhibernate\src\NHibernate\Event\Default\DefaultFlushEventLi­stener.cs:
> > > line 19
> > > at NHibernate.Impl.SessionImpl.Flush() in
> > > d:\CSharp\NH\NH\nhibernate\src\NHibernate\Impl\SessionImpl.cs: line
> 1489
> > > at NHibernate.Transaction.AdoTransaction.Commit() in
> > >
> d:\CSharp\NH\NH\nhibernate\src\NHibernate\Transaction\AdoTransaction.cs:
> > > line 190
> >
> > > It's pretty clear that NH is trying to add both keys of the Composite
> Blog
> > > identifier to the insert statement.  Switching from key-many-to-one to
> > > key-property produced the same result.  Is there any hints I can give
> it to
> > > indicate that the UserId is the same for both the Post and the Blog and
> > > there is no need to specify it twice?  Ideally there would be an
> > > insert="false" option on the many-to-on/column elements but I had no
> luck.
> > > I know composite-id issues are generally written off, but given the
> way SQL
> > > Azure Federations work, there is no other option.
> >
> > > I tried hacking this by adding <property name="Id" insert="false"
> > > update="false" unique="true" /> to Blog and <many-to-one name="Blog"
> > > column="BlogId" property-ref="Id" /> to Post.  This broke SchemaExport
> but
> > > otherwise appeared to work.  But this broke the association logic NH
> does
> > > when linking the Posts to the Blog upon hydration so it was a no-go.
> >
> > > I'm not familiar with custom user types.  Any chance this will address
> the
> > > issue?  Any other power user configurations that might solve this?
> >
> > > --
> > > You received this message because you are subscribed to the Google
> Groups
> > > "nhusers" 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/nhusers?hl=en.
> >
> > --
> > Itzik Saban
> >
> > my blog <http://itziksaban.blogspot.com/>- Hide quoted text -
> >
> > - Show quoted text -
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" 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/nhusers?hl=en.
>
>

-- 
Itzik Saban

my blog <http://itziksaban.blogspot.com/>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" 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/nhusers?hl=en.

Reply via email to