官方微信 手机客户端

澳洲ABC

搜索
查看: 1549|回复: 10

[IT] 向IT编程高手求助

[复制链接]

7

主题

20

帖子

63

积分

新手上路

Rank: 1

积分
63
发表于 2014-6-18 11:39:27 | 显示全部楼层 |阅读模式
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:
[ol]
  •    
  •       
  •      
  •       
  •       
  •       
  •      
  •    
  •    [/ol]复制代码
    Mapping file at child site:
    [ol]
  •    
  •    
  •       
  •       
  •    
  •   [/ol]复制代码
    Table Schema:
    [ol]
  • CREATE TABLE [dbo].[EBS_C_Category_Product] (
  •    [CategoryId] [uniqueidentifier] NOT NULL ,
  •    [ProductId] [uniqueidentifier] NOT NULL ,
  •    [DisplayOrder] [int] NOT NULL
  • ) [/ol]复制代码

    Code used to delete a child:
    [ol]
  •          objCategory.Products.Remove(objProduct);
  •          objProduct.Categories.Remove(objCategory);
  •          SessionManager.GetSession().SaveOrUpdate(objCategory); [/ol]复制代码

    Error message
    [ol]
  • Violation of PRIMARY KEY constraint 'PK_EBS_C_Category_Product'. Cannot insert duplicate key in object 'EBS_C_Category_Product'.
  • The statement has been terminated.
  • 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.
  • 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'.
  • The statement has been terminated.
  • Source Error:
  • Line 173:         CheckReaders();
  • Line 174:         Prepare(cmd);
  • Line 175:         return cmd.ExecuteNonQuery();
  • Line 176:      }
  • Line 177:
  •   
  • Source File: C:\temp\NHibernate-1.2.0.Beta2-debug\src\NHibernate\Impl\BatcherImpl.cs    Line: 175
  • Stack Trace:
  • [SqlException (0x80131904): Violation of PRIMARY KEY constraint 'PK_EBS_C_Category_Product'. Cannot insert duplicate key in object 'EBS_C_Category_Product'.
  • The statement has been terminated.]
  •    System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) +857386
  •    System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +734998
  •    System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +188
  •    System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1838
  •    System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +149
  •    System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +886
  •    System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +132
  •    System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) +415
  •    System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +135
  •    NHibernate.Impl.BatcherImpl.ExecuteNonQuery(IDbCommand cmd) in C:\temp\NHibernate-1.2.0.Beta2-debug\src\NHibernate\Impl\BatcherImpl.cs:175
  •    NHibernate.Impl.NonBatchingBatcher.AddToBatch(IExpectation expectation) in C:\temp\NHibernate-1.2.0.Beta2-debug\src\NHibernate\Impl\NonBatchingBatcher.cs:36
  •    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
  • [ADOException: could not update collection rows: [EBSimple.Commerce.DomainModel.Category.Products#2877d2b2-e890-48dd-8908-98ac0183c17e]]
  •    EBSimple.Core.NHibernateSessionManager.CommitTransaction() in C:\DNNDevEnvironment\DesktopModules\EBSimpleCommerce\Core\NHibernateSessionManager.cs:131
  •    EBSimple.Commerce.HttpModules.NHibernateSessionModule.CommitAndCloseSession(Object sender, EventArgs e) in C:\DNNDevEnvironment\DesktopModules\EBSimpleCommerce\HttpModules\NHibernateSessionModule.cs:43
  •    System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +92
  •    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +64
  •   
  • --------------------------------------------------------------------------------
  • Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.210 [/ol]复制代码
    Frank

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




  • 上一篇:招聘full time会计,Sydney city,适合需要第一份会计工作的人(已招到,谢谢)
    下一篇:我被四大Phone Interview

    0

    主题

    74

    帖子

    174

    积分

    正式会员

    Rank: 2

    积分
    174
    发表于 2014-6-18 12:20:41 | 显示全部楼层

    我没有用过NHibernate,也不知道这个东西是干什么用的。
    不过你得到的错误显然是由于试图在表中插入重复主键而引起,为什么你的delete会引起插入数据操作?
    不过,你的主键看起来应该是复合主键 categoryid + productid ,都是 GUID 类型,是不是你的表主键定义错误,仅仅把categoryid设置为主键了?
    回复 支持 反对

    使用道具 举报

    0

    主题

    13

    帖子

    46

    积分

    新手上路

    Rank: 1

    积分
    46
    发表于 2014-6-18 13:22:21 | 显示全部楼层


          
          [i]
          
         
    You only define the  CategoryId as primary Key, it should be composite primary key (categoryId + productId) according to what you described here.
    回复 支持 反对

    使用道具 举报

    7

    主题

    20

    帖子

    63

    积分

    新手上路

    Rank: 1

    积分
    63
    发表于 2014-6-18 14:20:21 | 显示全部楼层

    原帖由 jungle 于 2007-1-15 11:51 发表
    我没有用过NHibernate,也不知道这个东西是干什么用的。
    不过你得到的错误显然是由于试图在表中插入重复主键而引起,为什么你的delete会引起插入数据操作?
    不过,你的主键看起来应该是复合主键 categoryid ...

    thanks for your message. The primary key is composite one which includes both Ids.
    回复 支持 反对

    使用道具 举报

    7

    主题

    20

    帖子

    63

    积分

    新手上路

    Rank: 1

    积分
    63
    发表于 2014-6-18 14:53:50 | 显示全部楼层

    原帖由 袁氏行 于 2007-1-15 12:03 发表

          
    thanks a lot for coment.
    This "key" is not a primary key in database table. It tells Hibernate how to generate " Products" property of Category class. In my case, it instructs NHibernate to use CategoryId, which makes sense because one category can have multiple products. The mapping file is  correct. At this stage, I suspect this error is caused by a bug in NHibernate itself. I mean indexed collection is not ported over properly from Hibernate 2.1.7. Stragne enough, nobody from Nhibernate team confirms this, which makes my life like hell.
    [ 本帖最后由 Frankman 于 2007-1-15 12:27 编辑 ]
    回复 支持 反对

    使用道具 举报

    12

    主题

    122

    帖子

    316

    积分

    初入江湖

    Rank: 3Rank: 3

    积分
    316
    发表于 2014-6-18 16:01:43 | 显示全部楼层

    看cmd内容先
    回复 支持 反对

    使用道具 举报

    0

    主题

    74

    帖子

    174

    积分

    正式会员

    Rank: 2

    积分
    174
    发表于 2014-6-18 17:07:28 | 显示全部楼层

    刚刚google了一下,才发现原来NHibernate 也是一个用作Data Access Layer的第三方工具。
    TNND,不知道这些澳洲人怎么那么喜欢用这些数据库对象化的工具。我们公司用的是LGEN,我觉得也有不少毛病。它的事务控制机制就让我总觉得不能彻底相信,现在仍然有时候会出现同一个事务内的动作部分成功的情况。
    我只能猜:也许是你的这个NHibernate 对composite primary key 的支持本身有问题。你不如再加一个列category_product_id来做PK,那两个列只用作FK。
    回复 支持 反对

    使用道具 举报

    7

    主题

    20

    帖子

    63

    积分

    新手上路

    Rank: 1

    积分
    63
    发表于 2014-6-18 18:02:36 | 显示全部楼层

    原帖由 jungle 于 2007-1-15 12:49 发表
    刚刚google了一下,才发现原来NHibernate 也是一个用作Data Access Layer的第三方工具。
    TNND,不知道这些澳洲人怎么那么喜欢用这些数据库对象化的工具。我们公司用的是LGEN,我觉得也有不少毛病。它的事务控制 ...

    It wouldn't make any difference. It used to work without index column (two bags mapping). Pretty sure it's caused by NHibernate itself. It seems to me that Indexed column only works in Hibernate 3.
    Hibernate is not only a tool. It includes many data access best practices and even goes further. It's so popular in Java community and the same concept is getting more and more attention in .NET world. Have a look at DLINQ project at Microsoft and you will know what I mean, although it's crippled implementaiton of ORM.
    回复 支持 反对

    使用道具 举报

    0

    主题

    1万

    帖子

    1万

    积分

    江湖老手

    Rank: 6Rank: 6

    积分
    11054
    发表于 2015-7-5 04:08:43 | 显示全部楼层
    路过,过来看看
    回复 支持 反对

    使用道具 举报

    8

    主题

    1万

    帖子

    1万

    积分

    江湖老手

    Rank: 6Rank: 6

    积分
    10786
    发表于 2015-8-31 05:01:58 | 显示全部楼层
    帮你顶顶!!!
    回复 支持 反对

    使用道具 举报

    发表回复

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    联系客服 关注微信 下载APP 返回顶部 返回列表