Hi,
I'm doing my first work with Hibernate(3)+Oracle(9)+Tomcat(4.1) using the Ecplise IDE. This is my problem:
I have a table with a String DATE field, I'd like querying it by a substring of this field (eg:SELECT or GRUOP BY by mounth,or by day, etc).
I read in this forum that HSQL don't support string function like substring, so I would have to use native SQL, but this solution will not allow me to make projection on my table!
Some topics advise to use direct jdbc, but I'd like to use hibernate!
I tried to use a "UserType" class to map table's date field in a class that includes year, months and day fields (this is the code snippet):
public class DateH{ int year; int month; int day; public DateH(String StrDate) { StringTokenizer da; Integer AppD=null; da=new StringTokenizer(StrDate,"/"); while (da.hasMoreTokens()) { AppD=new Integer(da.nextToken()); year=AppD.intValue(); AppD=new Integer(da.nextToken()); month=AppD.intValue(); AppD=new Integer(da.nextToken()); day=AppD.intValue(); } } ... ...
and then I tried to make a query in order to get the values (the table name is Riga):
Query query1 = session.createQuery("select c from Riga as c where c.Date.year=\"2003\"");
Unfortunately this doesn't work; I wonder if the problem is in the definition my UserType DateTypeH, this is it's code:
public class DateTypeH implements UserType { public int[] sqlTypes() { return new int[] { Types.VARCHAR }; } public Class returnedClass() { return MaraUtil.DataH.class; } public boolean equals(Object x, Object y) throws HibernateException { return (x == y) || (x != null && y != null && (x.equals(y))); } public Object nullSafeGet(ResultSet inResultSet, String[] names, Object o)throws HibernateException, SQLException { String val = (String)Hibernate.STRING.nullSafeGet(inResultSet, names[0]); return new MaraUtil.DataH(val); } public void nullSafeSet(PreparedStatement inPreparedStatement, Object o, int i) throws HibernateException, SQLException { String val = ((MaraUtil.DataH)o).getData(); inPreparedStatement.setString(i, val); } public Object deepCopy(Object o) throws HibernateException { if (o==null) return null; return new MaraUtil.DataH(new String(String.valueOf(o))); } public boolean isMutable() { return false; } public Object assemble(Serializable cached, Object owner) { return deepCopy(cached); } public Serializable disassemble(Object value) { return (Serializable) deepCopy(value); } public int hashCode(Object x) throws HibernateException { return x.hashCode(); } public Object replace(Object original, Object target, Object owner) throws HibernateException { return original; } }
Any help will be appreciated... Many thanks!!
Mara
|