-->
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.  [ 1 post ] 
Author Message
 Post subject: Map a string table's field to more than one class attribute
PostPosted: Fri Feb 11, 2005 7:09 am 
Newbie

Joined: Fri Feb 11, 2005 5:36 am
Posts: 5
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.