Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

java.sql.Date problem

843830Sep 19 2009 — edited Sep 24 2009
Hi,
I have a little problem with retrieving date values from oracle which I couldn't completely understand. Can someone help?
I have an oracle database table with field type date > JPA entity is representing this table in my app server > I retrieve some values from this entity in a session bean > I inject this session bean in a servlet.
My problem is when I try to instantiate a DTO with field java.sql.Date in it [constructor myDTO(java.sql.Date date);] I get an error unless I convert my retrieved date filed into String first.

#1
// method in a servlet with injected sessionbean (EjbSessionBean)
public MyDTO methodToReturnMyDTO(){
        MyJpaEntity myJpaEntity = EjbSessionBean.getJpaEntity();
        java.sql.Date myDate = myJpaEntity.getDateField();  //assign date retrieved from entity
        MyDTO newDTO = new myDTO(myDate);
        return newDTO;
#2
// method in a servlet with injected sessionbean (EjbSessionBean)
public MyDTO methodToReturnMyDTO(){
        MyJpaEntity myJpaEntity = EjbSessionBean.getJpaEntity();
        java.sql.Date myDate = myJpaEntity.getDateField();
        MyDTO newDTO = new myDTO(java.sql.Date.valueOf(myDate.toString())); //first convert retrieved date into String then Date then assign
        return newDTO;
#1 doesn't work and #2 works.

In myDTO the date field is java.sql.Date type and in the JpaEntity the date filed is also a java.sql.Date type. However if I pass to myDTO constructor a date field retrieved from the JpaEntity I get an error. on the opposite before passing the date filed to myDTO constructor if I first convert this date into string and then instantiate java.sql.Date type with this String myDTO is created I get no problems. Is not it the same date value? What is the difference I get with this Date>String>Date conversion?

I've searched quite a bit but couldn't find an answer, any suggestions would be very much appreciated.
Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 22 2009
Added on Sep 19 2009
14 comments
2,463 views