Advertisement
Advertisement

新足迹

 找回密码
 注册
新足迹 门户 工作学习 查看内容

向IT编程高手求助

2007-1-15 08:56| 发布者: Frankman | 查看: 1594| 原文链接

I'm using NHibernate, which is a .NET port from Java Hibernate 2.1.7, and came across this issue. Hope someone help me out. Thanks a lot.

I have a bidirectional many-to-many mapping with index column. When I try to delete any child which is not the last one in the collection, deletion fails.


Mapping file at parent site:

  1.   <class name="EBSimple.Commerce.DomainModel.Category, EBSimple.Commerce.DomainModel" table="EBS_C_Category">
  2.       
  3.     <list name="Products"
  4.           table="EBS_C_Category_Product"
  5.           lazy="false"
  6.           cascade="save-update">
  7.       <key column="CategoryId"/>
  8.       <index column="DisplayOrder"/>
  9.       <many-to-many class="EBSimple.Commerce.DomainModel.Product, EBSimple.Commerce.DomainModel" column="ProductId"/>
  10.     </list>
  11.    
  12.   </class>
复制代码



Mapping file at child site:

  1.   <class name="EBSimple.Commerce.DomainModel.Product, EBSimple.Commerce.DomainModel" table="EBS_C_Product">

  2.     <bag name="Categories"
  3.          table="EBS_C_Category_Product"
  4.          inverse="true">
  5.       <key column="ProductId"/>
  6.       <many-to-many class="EBSimple.Commerce.DomainModel.Category, EBSimple.Commerce.DomainModel" column="CategoryId"/>
  7.     </bag>

  8.   </class>
复制代码



Table Schema:

  1. CREATE TABLE [dbo].[EBS_C_Category_Product] (
  2.    [CategoryId] [uniqueidentifier] NOT NULL ,
  3.    [ProductId] [uniqueidentifier] NOT NULL ,
  4.    [DisplayOrder] [int] NOT NULL
  5. )
复制代码




Code used to delete a child:


  1.          objCategory.Products.Remove(objProduct);
  2.          objProduct.Categories.Remove(objCategory);
  3.          SessionManager.GetSession().SaveOrUpdate(objCategory);
复制代码


Error message

  1. Violation of PRIMARY KEY constraint 'PK_EBS_C_Category_Product'. Cannot insert duplicate key in object 'EBS_C_Category_Product'.
  2. The statement has been terminated.

  3. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

  4. Exception Details: System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK_EBS_C_Category_Product'. Cannot insert duplicate key in object 'EBS_C_Category_Product'.
  5. The statement has been terminated.

  6. Source Error:


  7. Line 173:         CheckReaders();
  8. Line 174:         Prepare(cmd);
  9. Line 175:         return cmd.ExecuteNonQuery();
  10. Line 176:      }
  11. Line 177:
  12.   

  13. Source File: C:\temp\NHibernate-1.2.0.Beta2-debug\src\NHibernate\Impl\BatcherImpl.cs    Line: 175

  14. Stack Trace:


  15. [SqlException (0x80131904): Violation of PRIMARY KEY constraint 'PK_EBS_C_Category_Product'. Cannot insert duplicate key in object 'EBS_C_Category_Product'.
  16. The statement has been terminated.]
  17.    System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +857386
  18.    System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +734998
  19.    System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +188
  20.    System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1838
  21.    System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +149
  22.    System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +886
  23.    System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +132
  24.    System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +415
  25.    System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +135
  26.    NHibernate.Impl.BatcherImpl.ExecuteNonQuery(IDbCommand cmd) in C:\temp\NHibernate-1.2.0.Beta2-debug\src\NHibernate\Impl\BatcherImpl.cs:175
  27.    NHibernate.Impl.NonBatchingBatcher.AddToBatch(IExpectation expectation) in C:\temp\NHibernate-1.2.0.Beta2-debug\src\NHibernate\Impl\NonBatchingBatcher.cs:36
  28.    NHibernate.Persister.Collection.BasicCollectionPersister.DoUpdateRows(Object id, IPersistentCollection collection, ISessionImplementor session) in C:\temp\NHibernate-1.2.0.Beta2-debug\src\NHibernate\Persister\Collection\BasicCollectionPersister.cs:222

  29. [ADOException: could not update collection rows: [EBSimple.Commerce.DomainModel.Category.Products#2877d2b2-e890-48dd-8908-98ac0183c17e]]
  30.    EBSimple.Core.NHibernateSessionManager.CommitTransaction() in C:\DNNDevEnvironment\DesktopModules\EBSimpleCommerce\Core\NHibernateSessionManager.cs:131
  31.    EBSimple.Commerce.HttpModules.NHibernateSessionModule.CommitAndCloseSession(Object sender, EventArgs e) in C:\DNNDevEnvironment\DesktopModules\EBSimpleCommerce\HttpModules\NHibernateSessionModule.cs:43
  32.    System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +92
  33.    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64

  34.   
  35. --------------------------------------------------------------------------------
  36. Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210
复制代码


Frank


--------------------------------------------------------------------------------

[ 本帖最后由 Frankman 于 2007-1-15 10:50 编辑 ]
Advertisement
Advertisement


Advertisement
Advertisement
返回顶部