-->
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.  [ 13 posts ] 
Author Message
 Post subject: Hibernate 3 doesn't return the right type...
PostPosted: Fri Feb 25, 2005 7:33 am 
Beginner
Beginner

Joined: Mon Jan 31, 2005 7:53 am
Posts: 32
Location: Madrid - Spain
Hi mates, I have a problem that it seems some had some time ago, but unfortunately he didn't get any reply.

http://forum.hibernate.org/viewtopic.php?t=938622

In keeping with my problem in detail, I have two methods for controlling a Date field in my database:

Code:
   public java.util.Date getFinished ()
   {
       return ( java.util.Date ) this.finished;
   }

   public void setFinished (java.util.Date finished)
   {
       this.finished = ( java.util.Date ) finished;
   }


As you can see above, I have casted the returned and assigned values to ensure they are turned into the right type. However, when I invoke the Session.find method, it returns an object properly formed although the finished field contains a Timestamp object rather than a Date one as it is defined. No matter how many castings I do, the final assigned and returned type is Timestamp. This means a big problem when calling my equals method among others.

Do you know why is this happening?
Do you know any way to solve this problem?

Thanks to all for spending some of your time reading this... :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 25, 2005 9:50 am 
Beginner
Beginner

Joined: Mon Jan 31, 2005 7:53 am
Posts: 32
Location: Madrid - Spain
Although I haven't found a final solution, I've seen a link which may help many of you:

http://forum.hibernate.org/viewtopic.php?t=931971


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 25, 2005 2:16 pm 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
Show your mappings. That's where this work is being done.
I think Hibernate overrides your accessors, so the above code may not be being executed. Just get your mappings right.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 25, 2005 5:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
hibernate does *not* override any accessors - all your pojo code will be executed as normal.

/max

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 25, 2005 6:09 pm 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
Then I am wrong!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 5:04 am 
Beginner
Beginner

Joined: Mon Jan 31, 2005 7:53 am
Posts: 32
Location: Madrid - Spain
Well, I've solved my problem. The reason why it was failing was that if you use System.currentMillis() method to create a new java.util.Date object, the object created has milliseconds accuracy and that's not supported for Oracle Date type. So, it simply rounds to seconds accuracy. This means that if I retrieve this object and compare it with the one used to save it into the data base, they won't be equal.

Since I don't need that high accuracy, a possible solution as it's as simple as changing the getFinished methods as shown below:

Code:
public void setFinished (java.util.Date finished)
   {
       if ( finished != null)
       {   
           this.finished = new Date( finished.getTime() - ( finished.getTime() % 1000 ) );   
       }
   }


Now I make sure that time is stored with no milliseconds part. If it happens you need more accuracy then you have to use a Timestamp type, although I don't know much how it works on Oracle 9i.


Thanks to all for your help... :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 10:19 am 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
You might consider Timestamp on the DB side, even if you don't need that accuracy, in order to more simply match the data with the Java side. It doesn't cost you anything! I think its the right match for Date.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 11:24 am 
Beginner
Beginner

Joined: Mon Jan 31, 2005 7:53 am
Posts: 32
Location: Madrid - Spain
Correct me if I'm wrong, but as far as I know Oracle doesn't have any Timestamp type. At least, I didn't find any one...! So, I don't know a simpler way to solve my problem...

Thanks for comment...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 12:31 pm 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
Musicolo wrote:
Correct me if I'm wrong, but as far as I know Oracle doesn't have any Timestamp type.

I'm pretty sure it does. Refer to your docs, or google as I did:

http://www.oracle.com/technology/oramag ... 62sql.html


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 2:32 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
prior to 9i, it does not support TIMESTAMP, only DATE. 9i and 10g support both


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 8:02 am 
Beginner
Beginner

Joined: Mon Jan 31, 2005 7:53 am
Posts: 32
Location: Madrid - Spain
Wow! What a surprise for me! I was unaware of this.

Now, I wonder if there is any way Hibernate says automatically Oracle to insert into the dabase the current date (something like SYSDATE)...

I ask that because I have a setCreated() and getCreated() methods that have to insert and retrieve a object creation date. What I have now in my database is a Date field with a SYSDATE as default value. So when I create an object a leave the creation date field empty(null) so that the database do this task for me. This force me to make a Session.refresh() after inserting the a object when I need to access the creation date.

For example, if I insert a User object:

Code:
...
User user = new User();
user.setName( "Musicolo" );
....
session.save( user );
session.flush(); // Don't know why I have to do that manually
session.connection().commit() // Don't know have to do that manually

System.out.println( user.getCreated() ); // This prints: null

session.refresh( user );
System.out.prinln( user.getCreated() );// This prints: 01/03/2005 12:37:12.0


I konw I should use Transaction approach, but I don't know how to get that. Apart from this comment, do you know how to achived what I said at the begging?

Many thanks to all!


PS. If you are thinking about using System.currentmillis() to insert the Date, forget it, as this approach is not valid because this project is running in distributed environment and there is no gurantee all machines have the very same time.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 01, 2005 8:12 am 
Beginner
Beginner

Joined: Mon Jan 31, 2005 7:53 am
Posts: 32
Location: Madrid - Spain
I've thought this may help:

hibernate.cfg.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
      "-//Hibernate/Hibernate Configuration DTD//EN"
      "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="java:hibernate/SessionFactory">
       <!--Database conection data -->
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@a.b.c.d:port:sid</property>
        <property name="hibernate.connection.username">username</property>
        <property name="hibernate.connection.password">topsecret</property>
        <property name="hibernate.default_schema">my_schema</property>
      
      <!-- Connection pool -->
      <property name="hibernate.c3p0.min_size">5</property>
      <property name="hibernate.c3p0.max_size">20</property>
      <property name="hibernate.c3p0.timeout">1800</property>
      <property name="hibernate.c3p0.max_statements">50</property>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property>

        <!--Transaction strategy configuration-->
      <property name="jta.UserTransaction">java:comp/UserTransaction</property>
        <property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>

<!--Other properties to determine whether they are needed
        <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.OrionTransactionManagerLookup</property>
        <property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.JBossTransactionManagerLookup</property>
-->

      <!-- Other properties -->
        <property name="hibernate.cglib.use_reflection_optimizer">true</property>
        <property name="show_sql">false</property>
       
<!-- The properties are supposedly for allowing to write binary block bigger
     than 4KB in Oracle. Unfortunately, it doesn't work, at least in my tests.

      <property name="use_streams_for_binary">true</property>
      <property name="jdbc.batch_size">0</property>       
-->
       
        <!--Only for compliant ANSI databases like Oracle and Sysbase -->
        <property name="hibernate.max_fetch_depth">3</property>
       
             

    </session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject: Timestamping objects
PostPosted: Fri Feb 03, 2006 7:25 am 
Newbie

Joined: Fri Feb 03, 2006 7:21 am
Posts: 1
I think you can use a < timestamp > in the mapping file to map a property in your model to a time stamped column in the db, like this:

<timestamp name="LastUpdatedOn" column="LASTUPDATEDON" />

is that what you mean?

so when a model is persisted to the db the LastUpdatedOn property is stamped with the current time.


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