-->
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.  [ 4 posts ] 
Author Message
 Post subject: Problem with timestamp
PostPosted: Wed May 03, 2006 5:51 am 
Beginner
Beginner

Joined: Wed May 03, 2006 5:10 am
Posts: 32
Location: Monopoli - Italy
Hi,

I have two Master tables (Master and Master2) and one Cross table described by the following xml:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" schema="dbo">
<class name="ObjectModel.Test.Cross, ObjectModel" table="Test_Cross">
<composite-id>
<key-many-to-one name="Master" class="ObjectModel.Test.Master, ObjectModel" column="IdMaster1" />
<key-many-to-one name="Master2" class="ObjectModel.Test.Master2, ObjectModel" column="IdMaster2" />
</composite-id>
<timestamp name="UpdateTimestamp" column="UpdateTimestamp" unsaved-value="null" />
<property name="CreateUser" column="CreateUser" type="Int32" />
<property name="Guid" column="Guid" type="Guid" />
<property name="Prop1" column="Prop1" type="String" />
<property name="Prop2" column="Prop2" type="String" />
<property name="UpdateUser" column="UpdateUser" type="Int32" />
</class>
<class name="ObjectModel.Test.Master, ObjectModel" table="Test_Master">
<id name="Id" type="Int32" column="Id" access="field.pascalcase-m-underscore">
<generator class="identity" />
</id>
<property name="CreateTimestamp" column="CreateTimestamp" type="DateTime" />
<property name="CreateUser" column="CreateUser" type="Int32" />
<property name="Description" column="Description" type="String" />
<property name="Flag" column="Flag" type="Int16" />
<property name="Guid" column="Guid" type="Guid" />
<bag name="crosses" inverse ="false" cascade="all">
<key column="IdMaster1" />
<one-to-many class="ObjectModel.Test.Cross, ObjectModel" />
</bag>
<bag name="masterdetails" inverse ="false" cascade="all">
<key column="IdMaster" />
<one-to-many class="ObjectModel.Test.MasterDetail, ObjectModel" />
</bag>
<bag name="simplecrosses" inverse ="false" cascade="all">
<key column="IdMaster1" />
<one-to-many class="ObjectModel.Test.SimpleCross, ObjectModel" />
</bag>
<property name="UpdateTimestamp" column="UpdateTimestamp" type="DateTime" />
<property name="UpdateUser" column="UpdateUser" type="Int32" />
</class>
<class name="ObjectModel.Test.Master2, ObjectModel" table="Test_Master2">
<id name="Id" type="Int32" column="Id" access="field.pascalcase-m-underscore">
<generator class="identity" />
</id>
<property name="CreateTimestamp" column="CreateTimestamp" type="DateTime" />
<property name="CreateUser" column="CreateUser" type="Int32" />
<property name="Description" column="Description" type="String" />
<property name="Guid" column="Guid" type="Guid" />
<bag name="crosses" inverse ="false" cascade="all">
<key column="IdMaster2" />
<one-to-many class="ObjectModel.Test.Cross, ObjectModel" />
</bag>
<bag name="simplecrosses" inverse ="false" cascade="all">
<key column="IdMaster2" />
<one-to-many class="ObjectModel.Test.SimpleCross, ObjectModel" />
</bag>
<property name="UpdateTimestamp" column="UpdateTimestamp" type="DateTime" />
<property name="UpdateUser" column="UpdateUser" type="Int32" />
</class>
</hibernate-mapping>

I have no problems inserting new items in the Cross table, but updating an existing record I have an exception of primary key violation.

Antonella


Hibernate version: 1.0.2

Name and version of the database you are using: SQLServer 2005


Last edited by antoska on Thu May 04, 2006 8:50 am, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed May 03, 2006 9:36 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
There's not enough information in your post to answer it (the long mapping file doesn't count). Please fill-in the template you were asked to fill when posting.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 03, 2006 11:32 am 
Beginner
Beginner

Joined: Wed May 03, 2006 5:10 am
Posts: 32
Location: Monopoli - Italy
Hibernate version: 1.0.2

Code between sessionFactory.openSession() and session.close():
//USER INTERFACE
private void btnUpdateCross_Click(object sender, EventArgs e)
{

BizDomain.CentralModel.Test.Masters.LoadById(451);
ObjectModel.Test.Master master1 = BizDomain.CentralModel.Test.Masters[0];

ObjectModel.Test.Cross cross = master1.Crosses[0];
cross.Prop1 = "Prop prova test";
cross.Prop2 = "Prop 2";
BizDomain.CentralModel.Test.Masters.SaveOrUpdate();

}

public ItemBase SaveOrUpdate(ItemBase entity)
{
try
{
beginTransaction();

_session.SaveOrUpdate(entity);

commitTransaction();
}
catch (Exception ex)
{
rollbackTransaction();
..........
}
finally
{
disposeTransaction();
}
return entity;
}


Full stack trace of any exception that occurs:
Unable to insert duplicate key in object 'Test_Cross'.
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(Int32 expectedRowCount)
at NHibernate.Persister.EntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, SqlString sql, Object obj, ISessionImplementor session)


Name and version of the database you are using:
MS SQL Server 2000

The generated SQL (show_sql=true):
NHibernate: INSERT INTO dbo.Test_Cross (UpdateTimestamp, CreateUser, UpdateUser, Guid, Prop1, Prop2, IdMaster1, IdMaster2) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7)
@p0 = '03/05/2006 17.13.44'
@p1 = '0'
@p2 = '0'
@p3 = '00000000-0000-0000-0000-000000000000'
@p4 = 'Prop prova test'
@p5 = 'Prop 2'
@p6 = '451'
@p7 = '2'
A first chance exception of type 'NHibernate.ADOException' occurred in NHibernate.dll

Thanks
Antonella


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 04, 2006 6:44 am 
Beginner
Beginner

Joined: Wed May 03, 2006 5:10 am
Posts: 32
Location: Monopoli - Italy
Hi!
I solved the problem by myself:

originally the field UpdateTimestamp contained the NULL value.
I loaded the record then I modified the value of another field and executed a SaveOrUpdate() action. But... because the unsaved-value="null" option in the xml file, NHibernate considerated my record as a NEW record, so it tried to insert it in the DB!

Antonella


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.