-->
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.  [ 5 posts ] 
Author Message
 Post subject: Record versionning
PostPosted: Sun Jul 20, 2008 6:53 pm 
Newbie

Joined: Sun Jun 22, 2008 1:57 pm
Posts: 11
Hi,

When I insert new records to the database, the "version" field is assigned with the version "1" - I checked the object property value and that's what I expected- and when I commit the field in the DB is null !!!???

I used the generated="always" to avoid yo manage it manually (see the hbm.xml I pasted below). The generator works fine but not the version...

Is someone able to explain why the object property works fine but the field is null in the database?

Hibernate version: 1.2 for .NET 2.0

Mapping documents:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping  xmlns="urn:nhibernate-mapping-2.2" assembly="DataLayer" namespace="DataLayer.DataObjects">
  <class name="FREQUENCY" table="FREQUENCY" optimistic-lock="version" dynamic-update="true" dynamic-insert="true" >
    <id name="FREQUENCY_OID" column="FREQUENCY_OID" type="Int32">
      <generator class="hilo" />
    </id>
    <version column="FREQUENCY_RECORD_VERSION" name="FREQUENCY_RECORD_VERSION" type="Int32" generated="always" />
    <property name="FREQUENCY_CODE" type="String" length="15" not-null="true"/>
    <property name="FREQUENCY_DESCRIPTION" type="String" length="80" not-null="true"/>
    <property name="FREQUENCY_VALUE" type="Int32" not-null="true"/>
    <property name="FREQUENCY_UNIT" type="String" length="8" not-null="true"/>
  </class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
transaction.Begin();
foreach (String frequency in frequencies)
{
     // frequency.FREQUENCY_RECORD_VERSION = null

     // Insertion
     transaction.Save(frequency);

    // frequency.FREQUENCY_RECORD_VERSION  =1
}
transaction.Commit();

// But the field in the database is null!!!


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


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 26, 2008 3:31 pm 
Newbie

Joined: Sun Jun 22, 2008 1:57 pm
Posts: 11
Hi,

After some long and hard debuging (I'm a newbie with C# and Hibernate), I found that a versionning field with the attribute generated="always" set the hibernate flags generatedinsert to true and insert to false.

The "insert" flag filters fields added to the insert SQL statement, thus the versionning field is not inserted whith this mapping. It is inserted only when generated="never'. And in both cases, the object version number is correctly incremented, I found differences only about the insertion in the database.

I must be frank, I did not understand at all, but now its works. If someone can explain how it works and What I did not understood about the generated attribute it will be really appreciate.

C-Rock


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 26, 2008 5:26 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The generated="always" setting means that Hibernate expects the database to generate the value. I think this is clearly stated in the documentation:

http://www.hibernate.org/hib_docs/v3/re ... -generated


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 28, 2008 8:35 pm 
Newbie

Joined: Sun Jun 22, 2008 1:57 pm
Posts: 11
Hi nordborg,

Thank you for your answer, I already checked this before posting. You do not explain anything.

If the database generates always the version number, could you explain why the version field is valid in memory and null in the database when I use generated="always"?

Thank you,
C-Rock


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 5:30 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Probably because your database doesn't do any automatic generation of the version number and the default value for the colum is null. If generated="always" Hibernate will not try to insert values for that column since it expects the database to generate the value. After the insert Hibernate will issue a SELECT to load the value from the database.[/b]


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