-->
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.  [ 35 posts ]  Go to page Previous  1, 2, 3  Next
Author Message
 Post subject:
PostPosted: Mon Feb 02, 2004 2:01 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I wouldn't call it perfect, but probably the best that can be done using the current Java API :) We will need to keep in mind that something like this will break:

Code:
java.util.Date d = new java.util.Date()
Something s = session.load("from Something s where s.dateProperty = ?", d, Hibernate.TIMESTAMP).get(0);
s.getDateProperty().equals(d);

The problem I see is that the user thinks he has a java.util.Date but actually has a java.sql.Timestamp


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2004 5:48 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
gavin wrote:
Note that no self-respecting code should EVER call equals on a Timestamp. Its almost as bad as for Float.


Agreed!
BUT... do you realize that it is not obvious from the API that you will get a java.sql.Timestamp back instead of your original java.util.Date ?

Consider the following class:
Code:
  public class Message {
    private Date creationDateTime;
 
    public Date getCreationTime() {
      return creationDateTime;
    }
 
    public void setCreationTime( Date creationDateTime ) {
      this.creationDateTime = creationDateTime;;
    }
  }


The creationDateTime is supposed to record the date/time the message has been created. To persist it, I will have to map it using a timestamp, right ?

Code:
  <class name="message">
  [...]
    <property name="creationDateTime" type="timestamp" />
  [...]
  </class>


Now, suppose I create a new Message instance and set its creation time to the current time. Later - in the same session - I can compare this time with another java.util.Date without any problem:

Code:
  if( msg.getCreationTime().before(now) ) {
    <do something>
  }


BUT, if you reload the message object from the persistent store, the code above will always fail !
This because you don't have a java.util.Date anymore but a java.sql.Timestamp - so my code now behaves differently with transient objects and persistent/reloaded objects. Weird...


Stated differently: the Java<->DB transformation is not an actual function - it is not reversible.

This issue becomes even more important when you apply the DAO pattern where different implementations could be used. Suppose an Hibernate and an XML-file-based implementation - both will return data that looks like the same, but one has replaced dates with timestamps... How can you cope with this situation ?


So the question: how should I persist a java.util.Date with date+time precision so I can compare safely the vales with other java.util.Dates ?
AFAIK this is a very common case...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2004 5:58 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
eh???

Why on earth should before() fail?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2004 5:59 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well actually doing a comparison with .before(now) should be okay ... only equals is a problem. Which still sucks somehow.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2004 6:21 pm 
Proxool Developer
Proxool Developer

Joined: Tue Aug 26, 2003 10:42 am
Posts: 373
Location: Belgium
gloeglm wrote:
Well actually doing a comparison with .before(now) should be okay ... only equals is a problem. Which still sucks somehow.


That's right - sorry, took the wrong example :-(
Only the equals() will fail, the other methods are ok.

Cfr. above for an example breaking the equals().

This is because of the following code inside java.sql.Timestamp:
Code:
  public boolean equals(java.lang.Object ts) {
    if (ts instanceof Timestamp) {
      return this.equals((Timestamp)ts);
    }
    else {
      return false;
    }
  }


Hence this discussion ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 20, 2004 2:28 pm 
Regular
Regular

Joined: Tue Jan 06, 2004 3:32 pm
Posts: 80
Location: Munich, Germany
Sorry to re-open this old topic, but...

Not only does equals() fail, but compareTo() does also (at least on JDK 1.5)!

Note that compareTo() is used throughout the whole Collections framework, so TreeSet.add will fail, TreeMap.put will fail, ...

In my opinion, this is a major problem that should be solved somehow.

Regards,

Andreas


Top
 Profile  
 
 Post subject: It also makes it very hard to map collections...
PostPosted: Wed Aug 02, 2006 7:16 pm 
Beginner
Beginner

Joined: Wed Jan 19, 2005 6:07 am
Posts: 20
I have a mapping like this :

Code:
      <map name="workDays" table="sales" lazy="false" where="date is not null">
         <key column="period_report_id"></key>
         <map-key type="timestamp" column="date" ></map-key>
         <one-to-many class="no.lux.beans.SalesReport" />
      </map>


and the column is defined as a date-only-field.

When I want to retrieve values from the map created, I need to extract a java.util.Date-object from an Calendar-instance with all time-componenets set to null as the key, but since all keys in the map are of java.sql.Timestamp, I won't find my object.

I vote for the solution to actually return java.util.Date if this is specified in the mapping-document (specifying timastamp or java.util.Date both results in java.sql.Timestamp-objects).


Top
 Profile  
 
 Post subject: Prblm with DB "DATE" mapped to Hibernate "tim
PostPosted: Mon Sep 18, 2006 3:02 am 
Newbie

Joined: Mon Sep 18, 2006 2:43 am
Posts: 1
michael wrote:
You can use Date and use type="timestamp" in your mapping


Hi,

I am using DATE for the database field and type="timestamp" for the corrsponding mapping in the HBM . The java datatype is java.util.date

I am able to save a record into th DB with this setup. However when I
access the same record, update it and try to save it back, I have the following exception :

org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.xx.xx.Reason#4]; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.xx.xx.Reason#4]
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.xx.xx.Reason#4]
....................................
..................................
........ at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:1635)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2208)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2118)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


The query for the update is as follows:
Hibernate: update WW_APPMT_REASON set MODIFIED_DT=?, CREATED_BY=?, CREATED_DT=?, MODIFIED_BY=?, DESCRIPTION=? where REASON_ID=? and MODIFIED_DT=?
2006-09-18 11:46:25,765 [main] DEBUG org.hibernate.type.TimestampType - binding '2006-09-18 11:46:25' to parameter: 1
2006-09-18 11:46:25,765 [main] DEBUG org.hibernate.type.LongType - binding '20' to parameter: 2
2006-09-18 11:46:25,765 [main] DEBUG org.hibernate.type.TimestampType - binding '2006-09-18 11:45:22' to parameter: 3
2006-09-18 11:46:25,765 [main] DEBUG org.hibernate.type.LongType - binding '21' to parameter: 4
2006-09-18 11:46:25,765 [main] DEBUG org.hibernate.type.StringType - binding 'Vaccination' to parameter: 5
2006-09-18 11:46:25,765 [main] DEBUG org.hibernate.type.LongType - binding '6' to parameter: 6
2006-09-18 11:46:25,765 [main] DEBUG org.hibernate.type.TimestampType - binding '2006-09-18 11:45:22' to parameter: 7


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 16, 2007 11:06 pm 
Newbie

Joined: Tue Jan 16, 2007 10:52 pm
Posts: 1
I had code blow up in a related way.

My domain loaded a "Date" object. Hibernate loaded in a sql.Timestamp implementation.

The object was then used in some struts action code using the "compareTo" method.

Result: ClassCastException.

LHS = sql.Timestamp RHS = util.Date

Workaround:
Create a DateTimeUserType to ensure the persistance layer only deals with util.Date.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 30, 2007 11:53 pm 
Beginner
Beginner

Joined: Wed Feb 14, 2007 10:43 am
Posts: 21
Here's one problem I am facing with date/time. We have DB2 on Linux and date/timestamp fields were mapped to java.util.Date. This worked fine. Then we switched to DB2 on Mainframe and the app broke. Hibernate failed on date/time conversion. We changed the mapping to java.sql.Date and it worked.

What do you Hibernate experts think? Should I change all my existing mappings? Or should the IBM db2 driver should handle it? This is an important issue. I hope someone from Hibernate Team will answer this.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 01, 2007 8:52 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
hiberiowa wrote:
Here's one problem I am facing with date/time. We have DB2 on Linux and date/timestamp fields were mapped to java.util.Date. This worked fine. Then we switched to DB2 on Mainframe and the app broke. Hibernate failed on date/time conversion. We changed the mapping to java.sql.Date and it worked.

What do you Hibernate experts think? Should I change all my existing mappings? Or should the IBM db2 driver should handle it? This is an important issue. I hope someone from Hibernate Team will answer this.


Well, I'm not on the Hibernate Team, but I'll give it a shot :)

There are a few versions of the DB2 dialect, as DB2 implementations across platforms are not consistent (thanks IBM). Try switching your dialect to DB2390Dialect and see if that clears up the problem.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 01, 2007 8:57 am 
Beginner
Beginner

Joined: Wed Feb 14, 2007 10:43 am
Posts: 21
Thanks Ananasi. Yes, we did switch to DB2390Dialect, but that didn't solve THIS problem. Yes, we did have other problems with the generated SQL which was taken care by switching to DB2390Dialect, but not this one.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 01, 2007 9:02 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Well, that's a new one to me then. I remember what a pain in the butt working with DB2 on AS/400 was, and I don't envy you. ;)

I'll look a bit more into it and get back to you.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 01, 2007 9:05 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
What was the specific error/stacktrace when the error occurred?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 01, 2007 9:16 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
Check the mainframes' date format system var. You may have to write your own dialect to handle the mainframes' specific date format string. I've had to do this in the past, and it's not tricky at all, just extend DB2390Dialect and maybe register the column type differently.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 35 posts ]  Go to page Previous  1, 2, 3  Next

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.