-->
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.  [ 8 posts ] 
Author Message
 Post subject: SQL updates on selects
PostPosted: Thu Apr 06, 2006 5:29 pm 
Newbie

Joined: Thu Apr 06, 2006 4:01 pm
Posts: 7
Hibernate version: 3.0

Name and version of the database you are using: Oracle 10g


I have a problem with Date/Timestamp fields. My POJO class has an attribute of type java.util.Date. In the Oracle database the type of the fiel is DATE.

So, in my selects Hibernate returns Timestamp type from the database, but I really need use java.util.Date in this class, because I use it in a webservice, and Timestamp don't have a empty constructor. So in my set of this attribute, I do like this:

Code:
public void setTime(Date time) {
   if (time != null)
      this.time = new Date(time.getTime());
}


Then hibernate detects that the value was changed and do an update into database.

Is there any way to Hibernate returns the java.util.Date type from Oracle db ? Or may I disable this automatic detects from Hibernate? If I override the "equals()" method of my POJO class could I fix this ?


Thanks in advance.

[]
Anderson Araujo


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 06, 2006 6:29 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Use different method to get date for WS.
For example:
//for Hibernate
public void setTime(Date time) {
this.time = time;
}

//for WS
public java.util.Date getDate(){
if( time == null ) { return null;}
return new Date(time.getTime());
}

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 06, 2006 6:56 pm 
Newbie

Joined: Thu Apr 06, 2006 4:01 pm
Posts: 7
Hi, thanks for your reply.

I alredy try this, but I stiil get error on ws, I think it uses reflection to discover the types.

any other idea ?

tks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 06, 2006 7:39 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Well, just use util.Date in the class, H does convert them OK.
Please use post template and provide all the information to avoid guess work

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 8:13 am 
Newbie

Joined: Thu Apr 06, 2006 4:01 pm
Posts: 7
kgignatyev wrote:
Well, just use util.Date in the class, H does convert them OK.


The problem is that Hibernate returns a java.sql.Timestamp from db, wich is subclass of java.util.Date, but I need H returns java.util.Date from db.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 11:27 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
your setter should look like
public void setDate( java.util.Date v ){

}

and in the mapping you should say:
<property name="date" type="java.util.Date" ...

Please have a look at
http://www.hibernate.org/hib_docs/v3/re ... n-property
it tells about situation with dates specifically

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 1:30 pm 
Newbie

Joined: Thu Apr 06, 2006 4:01 pm
Posts: 7
my attribute in POJO class:

Code:
private java.util.Date time = new java.util.Date();

public java.util.Date getTime() {
   return time;
}

public void setTime(java.util.Date time) {
   this.time = time;
}


the mapping:

Code:
<class name="Customer" table="Customer">
   <property name="time"
         type="java.util.Date"
         column="TIME" />            
....


I'm still getting java.sql.Timestamp in "setTime()" method. I looked the reference but didn't find anything wrong.

tks again


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 07, 2006 3:49 pm 
Newbie

Joined: Thu Apr 06, 2006 4:01 pm
Posts: 7
I solve this creating a Custom Type wich always return a java.util.Date type.

Code:
   public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner) throws HibernateException, SQLException {
      if ( resultSet.wasNull() )
         return null;
      
      Timestamp ts =  resultSet.getTimestamp( names[0] );
      
      if ( ts != null )
         return new Date( ts.getTime() );

      return null;
   }


thx for all replies.

[]s


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