Hibernate version: 1.2.0Beta2
Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="HELM.Domains.Orderlines,HELM.Domains" table="orderlines" lazy="false">
<!--<id name="Orderid" column="OrderID" type="System.Int32" unsaved-value="any" >
<generator class="assigned"/>
</id>-->
<composite-id>
<!--<key-property name="WarehouseId" column="WarehouseID" type="System.Int32"/>
<key-property name="ItemId" column="ItemId" type="System.Int32"/>-->
<!--<key-many-to-one name="Items" class="HELM.Domains.Inventory, HELM.Domains">
<column name="Warehouseid"/>
<column name="ItemId"/>
</key-many-to-one>-->
<key-property name="Orderid" column="OrderID" type="System.Int32" />
<key-property name="OrderLineid" column="OrderLineID" type="System.Int32" />
</composite-id>
<!--<property column="OrderLineID" name="OrderLineid" type="System.Int32"/>-->
<property column="ItemId" type="System.Int32" name="ItemId" not-null="true"/>
<property column="WarehouseID" type="System.Int32" name="WarehouseId" not-null="true"/>
<!--<property column="OrderID" type="System.Int32" name="Orderid" />-->
<property column="Quantity" type="System.Int16" name="Quantity" />
<property column="Ship" type="System.Int16" name="Ship" />
<property column="Amount" type="System.Decimal" name="Amount" />
<property column="Tax" type="System.Decimal" name="Tax" />
<property column="Weight" type="System.Single" name="Weight" />
<property column="StatusDate" type="System.DateTime" name="StatusDate" />
<property column="ShipDate" type="System.DateTime" name="ShipDate" />
<property column="Status" type="System.String" name="Status" />
<property column="Tracking1" type="System.String" name="Tracking1" />
<property column="Tracking2" type="System.String" name="Tracking2" />
<property column="RetailPrice" type="System.Decimal" name="RetailPrice" />
<property column="WholesalePrice" type="System.Decimal" name="WholesalePrice" />
<property column="GroupItem" type="System.Int16" name="GroupItem" />
<property column="Volume" type="System.Decimal" name="Volume" />
<property column="Notes" type="System.String" name="Notes" />
<property column="ExtraCurrency1" type="System.Decimal" name="ExtraCurrency1" />
<property column="ExtraCurrency2" type="System.Decimal" name="ExtraCurrency2" />
<property column="ExtraCurrency3" type="System.Decimal" name="ExtraCurrency3" />
<property column="TaxRecID" type="System.Int32" name="TaxRecid" />
<property column="TrackingShipCo" type="System.String" name="TrackingShipCo" />
<property column="TaxRate" type="System.Double" name="TaxRate" />
<property column="MaxTaxAmt" type="System.Decimal" name="MaxTaxAmt" />
<property column="MaxOrderAmt" type="System.Decimal" name="MaxOrderAmt" />
<property column="CustomPrice" type="System.Decimal" name="CustomPrice" />
<property column="DiscountPrice" type="System.Decimal" name="DiscountPrice" />
<property column="DiscountDescription" type="System.String" name="DiscountDescription" />
<property column="ShipCost" type="System.Decimal" name="ShipCost" />
<property column="Aristo_Status" type="System.String" name="AristoStatus" />
<property column="First_Time_Orderitem" type="System.Byte" name="FirstTimeOrderitem" />
<property column="ActualWeight" type="System.Decimal" name="ActualWeight" />
<!-- Relationships-->
<!--<many-to-one name="Items" class="HELM.Domains.Inventory,HELM.Domains" cascade="none">
<column name="Warehouseid" />
<column name="ItemId"/>
</many-to-one>-->
</class>
</hibernate-mapping>
Full stack trace of any exception that occurs:Code:
19:07:58.936 [11] DEBUG NHibernate.Util.ADOExceptionReporter - could not insert: [HELM.Domains.Orderlines#HELM.Domains.Orderlines] [INSERT INTO orderlines (ItemId, WarehouseID, Quantity, Ship, Amount, Tax, Weight, StatusDate, ShipDate, Status, Tracking1, Tracking2, RetailPrice, WholesalePrice, GroupItem, Volume, Notes, ExtraCurrency1, ExtraCurrency2, ExtraCurrency3, TaxRecID, TrackingShipCo, TaxRate, MaxTaxAmt, MaxOrderAmt, CustomPrice, DiscountPrice, DiscountDescription, ShipCost, Aristo_Status, First_Time_Orderitem, ActualWeight, OrderID, OrderLineID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]
System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK_orderlines'. Cannot insert duplicate key in object 'orderlines'.
The statement has been terminated.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
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)
at NHibernate.Impl.NonBatchingBatcher.AddToBatch(IExpectation expectation)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session)
19:07:58.936 [11] WARN NHibernate.Util.ADOExceptionReporter - System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK_orderlines'. Cannot insert duplicate key in object 'orderlines'.
The statement has been terminated.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
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)
at NHibernate.Impl.NonBatchingBatcher.AddToBatch(IExpectation expectation)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session)
19:07:58.936 [11] ERROR NHibernate.Util.ADOExceptionReporter - Violation of PRIMARY KEY constraint 'PK_orderlines'. Cannot insert duplicate key in object 'orderlines'.
The statement has been terminated.
19:07:58.936 [11] ERROR NHibernate.Impl.SessionImpl - could not synchronize database state with session
NHibernate.ADOException: could not insert: [HELM.Domains.Orderlines#HELM.Domains.Orderlines][SQL: INSERT INTO orderlines (ItemId, WarehouseID, Quantity, Ship, Amount, Tax, Weight, StatusDate, ShipDate, Status, Tracking1, Tracking2, RetailPrice, WholesalePrice, GroupItem, Volume, Notes, ExtraCurrency1, ExtraCurrency2, ExtraCurrency3, TaxRecID, TrackingShipCo, TaxRate, MaxTaxAmt, MaxOrderAmt, CustomPrice, DiscountPrice, DiscountDescription, ShipCost, Aristo_Status, First_Time_Orderitem, ActualWeight, OrderID, OrderLineID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)] ---> System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK_orderlines'. Cannot insert duplicate key in object 'orderlines'.
The statement has been terminated.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
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)
at NHibernate.Impl.NonBatchingBatcher.AddToBatch(IExpectation expectation)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session)
--- End of inner exception stack trace ---
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql, Object obj, ISessionImplementor session)
at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session)
at NHibernate.Impl.ScheduledInsertion.Execute()
at NHibernate.Impl.SessionImpl.Execute(IExecutable executable)
at NHibernate.Impl.SessionImpl.ExecuteAll(IList list)
at NHibernate.Impl.SessionImpl.Execute()
Name and version of the database you are using: SQL Server 2000
The generated SQL (show_sql=true):Code:
19:07:58.936 [11] DEBUG NHibernate.SQL - INSERT INTO orderlines (ItemId, WarehouseID, Quantity, Ship, Amount, Tax, Weight, StatusDate, ShipDate, Status, Tracking1, Tracking2, RetailPrice, WholesalePrice, GroupItem, Volume, Notes, ExtraCurrency1, ExtraCurrency2, ExtraCurrency3, TaxRecID, TrackingShipCo, TaxRate, MaxTaxAmt, MaxOrderAmt, CustomPrice, DiscountPrice, DiscountDescription, ShipCost, Aristo_Status, First_Time_Orderitem, ActualWeight, OrderID, OrderLineID) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12, @p13, @p14, @p15, @p16, @p17, @p18, @p19, @p20, @p21, @p22, @p23, @p24, @p25, @p26, @p27, @p28, @p29, @p30, @p31, @p32, @p33); @p0 = '101300', @p1 = '6', @p2 = '1', @p3 = '', @p4 = '', @p5 = '', @p6 = '12', @p7 = '', @p8 = '', @p9 = '', @p10 = '', @p11 = '', @p12 = '120.0000', @p13 = '100.0000', @p14 = '', @p15 = '100.0000', @p16 = '', @p17 = '20.0000', @p18 = '', @p19 = '100.0000', @p20 = '', @p21 = '', @p22 = '', @p23 = '', @p24 = '', @p25 = '', @p26 = '', @p27 = '', @p28 = '', @p29 = '', @p30 = '', @p31 = '', @p32 = '5106350', @p33 = '1'
The problem I'm having is a pk violation (as shown above). What is inexplicable about this is that the INSERT
should be working! If I take the INSERT statement generated by nHibernate and run it in query analyzer directly it works fine. Here is the code where I do the insert:
Code:
//this transaction is for orders and orderlines only
//unsuccessful payment attempts will not result in a
//rollback of the orders or orderlines information
PersistenceManager.BeginTransaction();
HELMStatus theReturn = new HELMStatus();
try
{
//save orders
PersistenceManager.Save(order);
if (order.Orderlines != null)
{
//save orderlines
using (PersistenceManager<Orderlines, int> persistOrderlines = new PersistenceManager<Orderlines, int>())
{
//this counter will be for orderlineid
int orderlineID = 1;
foreach (Orderlines current in order.Orderlines)
{
//assign our orderid
current.Orderid = order.Orderid;
//added this to change the Inventory
current.ItemId = current.Items.Inventoryid;
current.WarehouseId = current.Items.Warehouseid;
current.OrderLineid = orderlineID;
persistOrderlines.Save(current);
//increment this after the save
orderlineID++;
}
}
}
else
{
//we need at least one orderline, this is a fatal error
throw new Exception("Order must have at least one OrderLine to be proccessed.");
}
}
catch (Exception ex)
{
//roll everything back if there is an error committing
//orderheader, orderline or payment
PersistenceManager.RollbackTransaction();
throw ex;
}
//commit our transaction if everything has succeeded
PersistenceManager.CommitChanges();
The current guess right now as to what is going on is that for some reason nHibernate is attempting 2 inserts with the same SQL in the transaction. When one fails, the transaction rolls back and that is why my manual insert attempt works. This error is not reproducable at will. The error only comes up ever few hours.