Hibernate Books

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Identity column - "Cannot update identity column"
PostPosted: Wed Sep 02, 2009 7:20 pm 
Newbie

Joined: Wed Sep 02, 2009 6:45 pm
Posts: 3
Hello,

I was trying to make one-to-many and many-to-many relation using NHibernate. I have Customer item with collection of Invoices (one-to-many) and Invoice with its Products (many-to-many). Ther is no problem with selecting all data from database(MSSQL), but when I`m trying to save Customer with Invoice with its Products I`m getting ADOException: "Cannot update identity column".

I am totally new to NHibernate and stuck on this problem :(

Can anybody help?
Maciek

Mapping files looks like this:
(Customer.hbm.xml)
<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="InvoiceNH"
assembly="InvoiceNH">
<!-- Mappings for class 'Customer' -->
<class name="Customer" table="Customer" lazy="false">
<!-- Identity mapping -->
<id name="Id">
<column name="Id" />
<generator class="native" />
</id>

<component name="Address">
<property name="Street" />
<property name="City" />
<property name="PostalCode" />
</component>
<!-- One-to-many mapping: Invoice -->
<bag name="Invoices" cascade="all-delete-orphan" lazy="false">
<key column="Id" />
<one-to-many class="Invoice" />
</bag>

<property name="Name" />
<property name="Telephon" />
</class>
</hibernate-mapping>


(Invoice.hbm.xml)
<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="InvoiceNH"
assembly="InvoiceNH">

<!-- Mappings for class 'Invoice' -->
<class name="Invoice" table="Invoice" lazy="false">
<!-- Identity mapping -->
<id name="Id">
<column name="Id" />
<generator class="native" />
</id>
<!-- Many-to-one mapping: Customer -->
<many-to-one name="Customer"
class="Customer"
column="CustomerId"
cascade="all" />
<!-- Many-to-many mapping: InvoiceProducts -->
<bag name="Products" table="InvoiceProducts" cascade="none" lazy="false">
<key column ="InvoiceId" />
<many-to-many class="Product" column="ProductId" />
</bag>
<!-- Simple mappings -->
<property name="Date" />
<property name="InvoiceNumber" />
</class>
</hibernate-mapping>

Exception code
NHibernate.ADOException was unhandled
Message="could not insert collection: [InvoiceNH.Customer.Invoices#14]"
Source="NHibernate"
StackTrace:
at NHibernate.Persister.Collection.AbstractCollectionPersister.Recreate(IPersistentCollection collection, Object id, ISessionImplementor session)
at NHibernate.Impl.ScheduledCollectionRecreate.Execute()
at NHibernate.Impl.SessionImpl.Execute(IExecutable executable)
at NHibernate.Impl.SessionImpl.ExecuteAll(IList list)
at NHibernate.Impl.SessionImpl.Execute()
at NHibernate.Impl.SessionImpl.Flush()
at NHibernate.Transaction.AdoTransaction.Commit()
at InvoiceNH.PersistenceManager.Save[T](T item) in C:\SVN\InvoiceNH\InvoiceNH\DataAccessLayer\PersistenceManager.cs:line 80
at InvoiceNH.Controller.SaveBusinessObjects() in C:\SVN\InvoiceNH\InvoiceNH\DomainLayer\Controller.cs:line 44
at InvoiceNH.Program.Main(String[] args) in C:\SVN\InvoiceNH\InvoiceNH\PresentationLayer\Program.cs:line 15
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.Data.SqlClient.SqlException
Message="Cannot update identity column 'Id'."
Source=".Net SqlClient Data Provider"
ErrorCode=-2146232060
Class=16
LineNumber=1
Number=8102
Procedure=""
Server="localhost"
State=1
(...)


Top
 Profile  
 
 Post subject: Re: Identity column - "Cannot update identity column"
PostPosted: Wed Sep 02, 2009 7:28 pm 
Newbie

Joined: Wed Sep 02, 2009 6:45 pm
Posts: 3
SQL queries get from Profiler:

exec sp_executesql N'INSERT INTO Customer (Street, City, PostalCode, Name, Telephon) VALUES (@p0, @p1, @p2, @p3, @p4); select SCOPE_IDENTITY()',N'@p0 nvarchar(15),@p1 nvarchar(8),@p2 nvarchar(6),@p3 nvarchar(10),@p4 nvarchar(14)',@p0=N'Kaprio 123/4',@p1=N'Rzeszow',@p2=N'03-982',@p3=N'Hotel',@p4=N'123 456-66-22'


exec sp_executesql N'INSERT INTO Invoice (CustomerId, Date, InvoiceNumber) VALUES (@p0, @p1, @p2); select SCOPE_IDENTITY()',N'@p0 int,@p1 datetime,@p2 nvarchar(13)',@p0=12,@p1='2009-09-03 00:19:44:000',@p2=N'FV/2009/02/15'


exec sp_executesql N'UPDATE Invoice SET Id = @p0 WHERE Id = @p1',N'@p0 int,@p1 int',@p0=12,@p1=102 (this update generate exception I suppose)


Top
 Profile  
 
 Post subject: Re: Identity column - "Cannot update identity column"
PostPosted: Thu Sep 03, 2009 5:43 pm 
Newbie

Joined: Wed Sep 02, 2009 6:45 pm
Posts: 3
I found problem. Customer.hbm.xml shoud look like this:

<?xml version="1.0"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="InvoiceNH"
assembly="InvoiceNH">
<!-- Mappings for class 'Customer' -->
<class name="Customer" table="Customer" lazy="false">
<!-- Identity mapping -->
<id name="Id">
<column name="Id" />
<generator class="native" />
</id>

<component name="Address">
<property name="Street" />
<property name="City" />
<property name="PostalCode" />
</component>
<!-- One-to-many mapping: Invoice -->
<bag name="Invoices" cascade="all-delete-orphan" lazy="false">
<key column="CustomerId" />
<one-to-many class="Invoice" />
</bag>


Best wishes!
Maciek


Top
 Profile  
 
 Post subject: Re: Identity column - "Cannot update identity column"
PostPosted: Wed Jul 25, 2012 2:41 pm 
Newbie

Joined: Wed Jul 25, 2012 2:38 pm
Posts: 1
Thanks for posting the solution!!! Had the same problem.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.