-->
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: Empty string different from null causes timestamp update
PostPosted: Fri Sep 10, 2004 4:49 pm 
Newbie

Joined: Thu Mar 11, 2004 4:00 pm
Posts: 5
Location: Canada
Hibernate version:
2.1.3
Mapping documents:
Code:
<hibernate-mapping default-cascade="none" auto-import="true">

   <class name="com.Data" table="TEST_TIMESTAMP" >
      <id name="id" type="string" column="ID" unsaved-value="null">
         <generator class="assigned" />
      </id>

      <timestamp column="LAST_UPDATE" name="lastUpdate"/>
      <property name="data" type="string" column="DATA" not-null="false" />
   </class>
   
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Code:
Data data = (Data)session.load(Data.class,"1");
// Value coming from presentation layer
data.setData("");
session.flush();

Full stack trace of any exception that occurs:

Name and version of the database you are using:
Oracle 9.2.0.3
The generated SQL (show_sql=true):
Code:
Hibernate: select data0_.ID as ID0_, data0_.LAST_UPDATE as LAST_UPD2_0_, data0_.DATA as DATA0_ from TEST_TIMESTAMP data0_ where data0_.ID=?
Hibernate: update TEST_TIMESTAMP set LAST_UPDATE=?, DATA=? where ID=? and LAST_UPDATE=?

Debug level Hibernate log excerpt:
Code:
16:10:32,121 DEBUG Loader:197 - processing result set
16:10:32,121 DEBUG Loader:405 - result row: 1
16:10:32,121 DEBUG Loader:536 - Initializing object from ResultSet: 1
16:10:32,137 DEBUG Loader:605 - Hydrating entity: com.Data#1
16:10:32,168 DEBUG TimestampType:68 - returning '10 septembre 2004 15:58:15' as column: LAST_UPD2_0_
16:10:32,168 DEBUG StringType:64 - returning null as column: DATA0_
16:10:32,183 DEBUG SessionImpl:1899 - Version: 2004-09-10 15:58:15.0
16:10:32,183 DEBUG Loader:226 - done processing result set (1 rows)
...
16:10:39,527 DEBUG AbstractEntityPersister:274 - com.Data.data is dirty
16:10:39,527 DEBUG SessionImpl:2522 - Updating entity: [com.Data#1]
16:10:39,543 DEBUG Versioning:26 - Incrementing: 2004-09-10 15:58:15.0 to 2004-09-10 16:10:39.527



Hi,

this is quite an obvious problem but it has a great impact on our application because we already have a lot of code and mappings.

An object containing a null property is returned from the database and then presented into a browser page.
If the text field corresponding to the property is empty, the property is updated with an empty string (instead of null).
The dirty checking detects a change and then increments the version (which I think is legitimate).

This means trouble for us because there is a very strict control on database changes and it can also affect optimistic concurrency because the row is considered as changed even if the data is the same since for tha database the empty string is stored as NULL.

Is there some "low cost" workaround to that ?
My idea is to subclass StringType and change the equals method to implement null = "". Then we can probably substitute the type of each property dynamically at session factory creation.
What would be the impact of that ?

I understand that the clean way would be to set the property to null but we can't afford that change right know.

Any suggestion would be greatly appreciated.

Daniel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 5:21 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
the impact is that "" is not the same as "null"...

if this is the only solution, i'd prefer altering the table and update all data to "" and add a not null constraint... but this is very ugly....

Is it struts that is returning "" instead of null?

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 13, 2004 9:07 am 
Newbie

Joined: Thu Mar 11, 2004 4:00 pm
Posts: 5
Location: Canada
Well, I don't know if it is specific to Oracle but you cannot insert "" into a column, Oracle will store it as NULL and of course will raise an exception if there is a not null constraint.

That's right, the empty string is returned by Struts with the get() method of DynaActionForm.

I guess I'll try the custom type because it's the fastest solution.

_________________
Daniel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 13, 2004 9:33 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
or make MyDynaForm extends DynaActionForm and play with the accessors

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


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.