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