-->
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.  [ 12 posts ] 
Author Message
 Post subject: Problem with milliseconds in DateTime structure
PostPosted: Wed Oct 05, 2005 3:20 am 
If I try store DateTime structure into the Database then millisecond value is always 000 even if original value is for example 231, other stored values from DateTime structure are Ok.

Is This some bug in hibernate or can me somebody help with thic problem?


Top
  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 6:30 am 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
This happens since the resulotion of the datatime in the database is lower than the resulotion of it in .Net
In essense, the database can't save the milliseconds values.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 9:04 am 
Newbie

Joined: Wed Oct 05, 2005 8:57 am
Posts: 1
I experience the same problem as PDu describes. I am pretty sure my problem isn't caused by database resolution being low, since I am using the timestamp type in Oracle 10g which to my knowledge has 6 digits for milliseconds.

I have also tried using regular ADO.NET to read/write datetime instances to the database. This yielded the expected result, where the milleseconds would be persisted and read back.

Any other ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 6:33 am 
Contributor
Contributor

Joined: Thu May 12, 2005 9:45 am
Posts: 593
Location: nhibernate.org
It may be due to the fact that DateTime.ToString() don't include the milliseconds...

_________________
Pierre Henri Kuaté.
Get NHibernate in Action Now!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 8:55 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
You should map the column using type="timestamp", this will tell NHibernate to use the maximum possible precision.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 13, 2006 1:08 pm 
Newbie

Joined: Wed Feb 22, 2006 3:57 pm
Posts: 4
Sergey,

I have defined the following mapping:

<timestamp name="Timestamp" column="`TIME_STAMP`"/>

But using Oracle 10G Express Edition the millisecond is always 000.

Is there a way to fix this issue?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 13, 2006 6:29 pm 
Regular
Regular

Joined: Fri Jul 29, 2005 9:46 am
Posts: 101
Hi!
I agree with KPixel:
Quote:
It may be due to the fact that DateTime.ToString() don't include the milliseconds...


I think the offending code is in : src\NHibernate\Type\DateTimeType.cs (or if you prefer in TimestampType.cs)

In the method:

Code:
public override string ObjectToSQLString( object value )
{
         return "'" + value.ToString() + "'";
}


IMHO it should be something like (but I haven't tested it):

Code:
public override string ObjectToSQLString( object value )
{
   return "'" + value.ToString("yyyy/MM/dd HH:mm:ss.fff")+"'";
}


The trick i think, is in the ".fff". I have had this problem with other object relational mappers... its a slight, but pretty common mistake.


Last edited by luxspes on Sat May 13, 2006 6:42 pm, edited 3 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sat May 13, 2006 6:39 pm 
Regular
Regular

Joined: Fri Jul 29, 2005 9:46 am
Posts: 101
I am also a little worried about this... what happens if my particular database doesn't like the default implementation of ObjectToSQLString ? Do I have to create a pariticular NHibernateType? (Or... Is there a way to override it in a central location? (perhaps in Driver or Dialect?))

AFAIK the DateTime.ToString() is "culture dependant" that is it generates the string representation in a "based on the current culture", so I think there could be problems if the culture in the application server or in the smartclient is different than the one in the database server (for example if the user plays with his particular computer configuration... and changes the order of the the date, form year/month/day to year/day/month the 2 of March could become the 3 of February)

So... IMHO all the DateTime related types should generate SQL represetantion (inside ObjectToSQLString) using a fixed format string, so that the result is always the same (regardless of the culture)... what do you think?


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 14, 2006 7:57 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
ObjectToSQLString and DateTime.ToString() are only used when inserting literal values into SQL (for example, when using literal values instead of parameters in HQL). If you use parameters, the database driver formats the value.

As to why the timestamp's milliseconds are set to 000, it looks like it's some Oracle-related problem - maybe your column is actually set to only have up to second precision, or something like that.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 14, 2006 9:17 am 
Newbie

Joined: Wed Feb 22, 2006 3:57 pm
Posts: 4
From Oracle, I can run this query: UPDATE HOSTS SET TIME_STAMP = SYSTIMESTAMP;

TIME_STAMP field gets correctly updated with milliseconds.

So I'm not sure it an Oracle problem.

Here is the mapping:
<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">

<class name="Eforce.DOCSRunner.Component.Modules.Host,DOCSRunner.Component.Implement" table="HOSTS">
<cache usage="read-write"/>
<id name="Id" column="ID" type="Int32" unsaved-value="0">
<generator class="native">
<param name="sequence">HOSTS_GENERATOR</param>
</generator>
</id>
<timestamp name="Timestamp" column="`TIME_STAMP`"/>
<property name="Name" column="`NAME`" type="String" length="50" />
<property name="Port" column="`PORT`" type="Int32"/>
<property name="Address" column="`ADDRESS`" type="String" length="50" />
<property name="ProgIdPackager" column="`PROG_ID_PACKAGER`" type="String" length="50" />
<property name="ProgIdExecutor" column="`PROG_ID_EXECUTOR`" type="String" length="50" />
<property name="Service" column="`SERVICE`" type="String" length="50" />
<many-to-one name="Module" column="`MODULE`" class="Eforce.DOCSRunner.Component.Modules.Module,DOCSRunner.Component.Implement" cascade="none"/>
<property name="Disabled" column="`DISABLED`" type="Boolean" />
</class>

</hibernate-mapping>


Here is the log from NHibernate:
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.Impl.BatcherImpl - Building an IDbCommand object for the SqlString: INSERT INTO HOSTS ("PROG_ID_EXECUTOR", "TIME_STAMP", "PROG_ID_PACKAGER", "PORT", "DISABLED", "ADDRESS", "NAME", "SERVICE", "MODULE", ID) VALUES (:"PROG_ID_EXECUTOR", :"TIME_STAMP", :"PROG_ID_PACKAGER", :"PORT", :"DISABLED", :"ADDRESS", :"NAME", :"SERVICE", :"MODULE", :ID)
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.Persister.EntityPersister - Dehydrating entity: [Eforce.DOCSRunner.Component.Modules.Host#2]
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.Type.StringType - binding null to parameter: 0
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.Type.TimestampType - binding '9:15 AM' to parameter: 1
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.Type.StringType - binding null to parameter: 2
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.Type.Int32Type - binding '8203' to parameter: 3
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.Type.BooleanType - binding 'False' to parameter: 4
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.Type.StringType - binding 'HTTP://SKIPPER-MASTER:8203/DBSynchro' to parameter: 5
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.Type.StringType - binding 'SKIPPER-MASTER' to parameter: 6
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.Type.StringType - binding 'JobDBSynchro' to parameter: 7
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.Type.Int32Type - binding '2' to parameter: 8
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.Type.Int32Type - binding '2' to parameter: 9
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.SQL - INSERT INTO HOSTS ("PROG_ID_EXECUTOR", "TIME_STAMP", "PROG_ID_PACKAGER", "PORT", "DISABLED", "ADDRESS", "NAME", "SERVICE", "MODULE", ID) VALUES (:p0, :p1, :p2, :p3, :p4, :p5, :p6, :p7, :p8, :p9)
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.SQL - :p0 = ''
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.SQL - :p1 = '5/14/2006 9:15:02 AM'
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.SQL - :p2 = ''
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.SQL - :p3 = '8203'
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.SQL - :p4 = 'False'
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.SQL - :p5 = 'HTTP://SKIPPER-MASTER:8203/DBSynchro'
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.SQL - :p6 = 'SKIPPER-MASTER'
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.SQL - :p7 = 'JobDBSynchro'
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.SQL - :p8 = '2'
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.SQL - :p9 = '2'
2006-05-14 09:15:02,071 [4920] DEBUG NHibernate.Impl.BatcherImpl - Closed IDbCommand, open IDbCommands :0

I have also tested with second cache level disabled.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 14, 2006 12:10 pm 
Regular
Regular

Joined: Fri Jul 29, 2005 9:46 am
Posts: 101
Hi!
Maybe:
http://msdn.microsoft.com/library/defau ... access.asp
Can help you locate the source of the error... it even has a section named "Using Tracing to Debug a Parameter Binding Problem"


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 1:15 pm 
Newbie

Joined: Mon Jun 12, 2006 1:06 pm
Posts: 3
I think I can help out here: Don't use Microsoft's / .Net's standard Oracle provider.
Since switching to Oracle's ODPNet driver, I'm successfully saving and retrieving the milliseconds....
(Once you've got a suitable Oracle client installed,
a) change your hibernate.connection.driver_class to NHibernate.Driver.OracleDataClientDriver instead of NHibernate.Driver.OracleClientDriver, and
b) swap the "server" in the connection string for a "Data Source" specifying the SID.)

Hope that cleans this up (for future Googlers)!
Mike.


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