Craig,
A work colleague also suggested that it could be the underlying java code.
I ran the following tests :
Code:
try {
Date date = new Date("");
System.out.println("date is " + date.toString());
}
catch (Exception e) {
System.out.println("exception is " + e.getMessage());
}
This gave me an exception : exception is null
I also tried :
Code:
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date date = sdf.parse("");
System.out.println("date is " + date.toString());
}
catch (Exception e) {
System.out.println("exception is " + e.getMessage());
}
This gave me : exception is Unparseable date: ""
It appears that Java will generate an exception if you attempt to convert an empty string to a date (not an unreasonable response !!!!!).
I then tried a few tests with a JDBC connection :
The database column is declared as a date and contained an empty string.
I removed the varchar from the select statement .....
eg Select d_received from xyz
When I accessed the column as a string :
Code:
rs.getString(23)
Then I got an empty string (and when the column contains a date, I get the date as a string).
When I accessed the column as a date :
Code:
rs.getDate(23)
I got an exception : java.lang.IllegalArgumentException
I then returned back to my Hibernate mapping.
I modified the UserType to return CHAR and then VARCHAR (instead of date) and I got the default date back for an empty string (when accessing it as a string (rs.getString(names[0])),
If I declared the type as a Date
Code:
public int[] sqlTypes() {
return new int[] { Types.DATE };
}
Then if I accessed the column as a string I got the default date (1970-01-01 00:00:00.0)
If I accessed the column as a date :
Code:
String val = rs.getDate(names[0]).toString();
Then I got the default date minus the time (1970-01-01).
In conclusion, I do not appear to be able to get an empty string out of Hibernate (using both createCriteria and createQuery). I believe this is because of the fact that Hibernate accesses the column directly using the column name provided in the mapping file :
Code:
<property name="DTo" column="d_to" type="wd.qmdb.hibernate.TrimmedDateString"/>
and so to my final attempt (as a sudden idea from doumenting this) .......
I decided to try and declare the column as varchar(d_to) :
Code:
<property name="DTo" column="varchar(d_to)" type="wd.qmdb.hibernate.TrimmedDateString"/>
Expecting this to possibly work for just the read (and to fail on inserts etc) ....
But no, I got an exception : java.lang.Exception
The resulting HQL was :
Code:
clclientpe0_.varchar(d_to) as varchar(7_,
as opposed to :
Code:
clclientpe0_.d_to as d_to
If anyone has any other suggestions, can you please let me know ?????
Surely, I can not be the only person to have to contend with problem ?????
Thanks,
Mike.