These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: BUG in 1.2.0.Alpha1: idbag with identity key
PostPosted: Wed Jul 05, 2006 2:09 pm 
Newbie

Joined: Tue Jul 04, 2006 7:33 pm
Posts: 2
Location: São Paulo, Brazil
I am quite a newbie using NHibernate, but I am pretty sure that there is an error in the implementation of BasicCollectionPersister and/or AbstractCollectionPersister.

I was trying to use the <idbag> mapping, with no success. This is the mapping:

Code:
    <idbag name="Preferencias" table="PREFERENCIA_USUARIO">
      <collection-id column="ID_PREFERENCIA_USUARIO" type="Int32">
        <generator class="identity"/>
      </collection-id>
      <key column="ID_USUARIO" />
      <many-to-many
        class="Sky.Enterprise.VO.Entity.PreferenciaVO, Sky.Enterprise.VO"
        column="ID_PREFERENCIA"
      />
    </idbag>

And the following error was occurring:

Code:
System.InvalidCastException occurred
  Message="Failed to convert parameter value from a Object to a Int32."
  Source="System.Data"
  StackTrace:
       at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType)
       at System.Data.SqlClient.SqlParameter.GetCoercedValue()
       at System.Data.SqlClient.SqlParameter.Validate(Int32 index)
       at System.Data.SqlClient.SqlCommand.BuildParamList(TdsParser parser, SqlParameterCollection parameters)
       at System.Data.SqlClient.SqlCommand.BuildExecuteSql(CommandBehavior behavior, String commandText, SqlParameterCollection parameters, _SqlRPC& rpc)
       at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
       at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
       at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
       at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
       at NHibernate.Impl.BatcherImpl.ExecuteNonQuery(IDbCommand cmd) in C:\\NHibernate\1.2.0.alpha1\Src\Main\Impl\BatcherImpl.cs:line 208


After some debugging, I understood what was happening: NHibernate was trying to assign a value to the field PREFERENCIA_USUARIO.ID_PREFERENCIA_USUARIO, which is an IDENTITY Sql Server field.

The value being assigned was NHibernate.Id.IdentityGenerator.

It seems that AbstractCollectionPersister and BasicCollectionPersister are not dealing correctly with IDENTITY PK's (unlike SingleTableEntityPersister, which correctly generates an insert without the IDENTITY field).

I have made the following changes, to allow the insertion of the <idbag> mapping:

- NHibernate.Persister.Collection.BasicCollectionPersister.GenerateInsertRowString method

Replaced
Code:
if (hasIdentifier)
with
Code:
if (hasIdentifier && !(IdentifierGenerator is NHibernate.Id.IdentityGenerator))


- NHibernate.Persister.Collection.AbstractCollectionPersister.Recreate method

Same as above.

These changes seemed to work, but I cannot tell for sure that it is THE right way to do it...

Any comments? :-)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 05, 2006 2:44 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
This is a known bug, NH-364 in JIRA. I believe the fix is not so trivial but I don't remember the details right now.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 05, 2006 4:56 pm 
Newbie

Joined: Tue Jul 04, 2006 7:33 pm
Posts: 2
Location: São Paulo, Brazil
Yeah, I've just seen the JIRA issue... Sorry about posting about a bug already known...

Although the modification I've made has fixed my specific problem, I can easily understand that perhaps a more "robust" fix must be made...

But at least it is a temporary solution... I hope it helps somebody else out there... :-)

BTW, this new version of NHibernate is great, I really appreciate the generics support.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 20, 2007 11:51 am 
Newbie

Joined: Thu Jun 14, 2007 11:26 am
Posts: 16
rsenna,

I was having the same issue with version 1.2.0.GA. I tried the code change you suggested but now I get this exception:

{NHibernate.ADOException: could not insert collection rows: [Finning.CustomerPortal.Entity.NewUser.Roles#1] ---> System.IndexOutOfRangeException: Invalid index 2 for this SqlParameterCollection with Count=2.
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.Int32Type.Set(IDbCommand cmd, Object value, Int32 index) in C:\nhib_src\nhibernate\src\NHibernate\Type\Int32Type.cs:line 57
at NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index) in C:\nhib_src\nhibernate\src\NHibernate\Type\NullableType.cs:line 197
at NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, Boolean[] settable, ISessionImplementor session) in C:\nhib_src\nhibernate\src\NHibernate\Type\NullableType.cs:line 138
at NHibernate.Type.ManyToOneType.NullSafeSet(IDbCommand st, Object value, Int32 index, Boolean[] settable, ISessionImplementor session) in C:\nhib_src\nhibernate\src\NHibernate\Type\ManyToOneType.cs:line 62
at NHibernate.Persister.Collection.AbstractCollectionPersister.WriteElement(IDbCommand st, Object elt, Int32 i, ISessionImplementor session) in C:\nhib_src\nhibernate\src\NHibernate\Persister\Collection\AbstractCollectionPersister.cs:line 610
at NHibernate.Persister.Collection.AbstractCollectionPersister.InsertRows(IPersistentCollection collection, Object id, ISessionImplementor session) in C:\nhib_src\nhibernate\src\NHibernate\Persister\Collection\AbstractCollectionPersister.cs:line 1136
--- End of inner exception stack trace ---
at NHibernate.Persister.Collection.AbstractCollectionPersister.InsertRows(IPersistentCollection collection, Object id, ISessionImplementor session) in C:\nhib_src\nhibernate\src\NHibernate\Persister\Collection\AbstractCollectionPersister.cs:line 1161
at NHibernate.Impl.ScheduledCollectionUpdate.Execute() in C:\nhib_src\nhibernate\src\NHibernate\Impl\ScheduledCollectionUpdate.cs:line 78
at NHibernate.Impl.SessionImpl.Execute(IExecutable executable) in C:\nhib_src\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 3280
at NHibernate.Impl.SessionImpl.ExecuteAll(IList list) in C:\nhib_src\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 3259
at NHibernate.Impl.SessionImpl.Execute() in C:\nhib_src\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 3223
at NHibernate.Impl.SessionImpl.Flush() in C:\nhib_src\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 3057
at NHibernateTest.Program.Main(String[] args) in D:\CHumber\My Documents\Visual Studio 2005\Projects\NHibernateTest\NHibernateTest\Program.cs:line 44}

Did you ever come across this?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 20, 2007 11:51 am 
Newbie

Joined: Thu Jun 14, 2007 11:26 am
Posts: 16
rsenna,

I was having the same issue with version 1.2.0.GA. I tried the code change you suggested but now I get this exception:

{NHibernate.ADOException: could not insert collection rows: [xxx.Entity.NewUser.Roles#1] ---> System.IndexOutOfRangeException: Invalid index 2 for this SqlParameterCollection with Count=2.
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.Int32Type.Set(IDbCommand cmd, Object value, Int32 index) in C:\nhib_src\nhibernate\src\NHibernate\Type\Int32Type.cs:line 57
at NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index) in C:\nhib_src\nhibernate\src\NHibernate\Type\NullableType.cs:line 197
at NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object value, Int32 index, Boolean[] settable, ISessionImplementor session) in C:\nhib_src\nhibernate\src\NHibernate\Type\NullableType.cs:line 138
at NHibernate.Type.ManyToOneType.NullSafeSet(IDbCommand st, Object value, Int32 index, Boolean[] settable, ISessionImplementor session) in C:\nhib_src\nhibernate\src\NHibernate\Type\ManyToOneType.cs:line 62
at NHibernate.Persister.Collection.AbstractCollectionPersister.WriteElement(IDbCommand st, Object elt, Int32 i, ISessionImplementor session) in C:\nhib_src\nhibernate\src\NHibernate\Persister\Collection\AbstractCollectionPersister.cs:line 610
at NHibernate.Persister.Collection.AbstractCollectionPersister.InsertRows(IPersistentCollection collection, Object id, ISessionImplementor session) in C:\nhib_src\nhibernate\src\NHibernate\Persister\Collection\AbstractCollectionPersister.cs:line 1136
--- End of inner exception stack trace ---
at NHibernate.Persister.Collection.AbstractCollectionPersister.InsertRows(IPersistentCollection collection, Object id, ISessionImplementor session) in C:\nhib_src\nhibernate\src\NHibernate\Persister\Collection\AbstractCollectionPersister.cs:line 1161
at NHibernate.Impl.ScheduledCollectionUpdate.Execute() in C:\nhib_src\nhibernate\src\NHibernate\Impl\ScheduledCollectionUpdate.cs:line 78
at NHibernate.Impl.SessionImpl.Execute(IExecutable executable) in C:\nhib_src\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 3280
at NHibernate.Impl.SessionImpl.ExecuteAll(IList list) in C:\nhib_src\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 3259
at NHibernate.Impl.SessionImpl.Execute() in C:\nhib_src\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 3223
at NHibernate.Impl.SessionImpl.Flush() in C:\nhib_src\nhibernate\src\NHibernate\Impl\SessionImpl.cs:line 3057
at NHibernateTest.Program.Main(String[] args) in D:\CHumber\My Documents\Visual Studio 2005\Projects\NHibernateTest\NHibernateTest\Program.cs:line 44}

Did you ever come across this?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.