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.  [ 4 posts ] 
Author Message
 Post subject: ClassCastException in nullSafeGet of custom UserType class
PostPosted: Fri Jul 21, 2006 5:00 pm 
Newbie

Joined: Sun Jun 04, 2006 7:02 am
Posts: 7
Location: Hoboken
For some reason, the object passed to nullSafeGet is my persistent class, not the custom attribute. I'm not sure why. Any help?

Hibernate version:
3

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.marsh.common.account">
<class name="CurrencyDVO" table="currency">
<id name="id" type="long" unsaved-value="null">
<generator class="native"/>
</id>
<property name="currencyName" column="NAME" type="string" length="28"/>
<property name="currencyCode" column="CODE" type="string" length="3"/>
<property name="conversionRate" column="CONVERSION_RATE" type="double"/>
<property name="conversionDate" column="CONVERSION_DATE" type="com.marsh.common.account.DayType"/>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Session session = HibernateFactory.openSession();
Query q = session.createQuery("from CurrencyDVO where conversionDate = ? ");
q.setParameter(0, new Day(2006,6,27));
List results = q.list();
System.out.println("" + q);
session.flush();
HibernateFactory.close(session);
HibernateFactory.closeFactory();

Full stack trace of any exception that occurs:
Exception in thread "main" java.lang.ClassCastException: com.marsh.common.account.CurrencyDVO
at com.marsh.common.account.DayType.nullSafeGet(DayType.java:65)
at org.hibernate.type.CustomType.nullSafeGet(CustomType.java:104)
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:81)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:1983)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1372)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1300)
at org.hibernate.loader.Loader.getRow(Loader.java:1197)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:569)
at org.hibernate.loader.Loader.doQuery(Loader.java:689)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2145)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
at org.hibernate.loader.Loader.list(Loader.java:2024)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:392)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:333)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1114)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at com.marsh.common.account.ExchangeRate.main(ExchangeRate.java:39)


Name and version of the database you are using:
Oracle 10

Code for custom UserType:
/*
* Created on Jun 15, 2006
*
* Copyright 2006, Marsh
*/
package com.marsh.common.account;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Date;

import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

/**
* @author etrembic
*
*/
public class DayType implements UserType {
private static final int[] types = {Types.DATE};

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#sqlTypes()
*/
public int[] sqlTypes() {
return types;
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#returnedClass()
*/
public Class returnedClass() {
return Day.class;
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#equals(java.lang.Object, java.lang.Object)
*/
public boolean equals(Object arg0, Object arg1) throws HibernateException {
if (arg0 instanceof Day) {
return ((Day) arg0).equals(arg1);
}
return false;
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#hashCode(java.lang.Object)
*/
public int hashCode(Object arg0) throws HibernateException {
if (arg0 instanceof Day) {
return ((Day) arg0).hashCode();
}
return 0;
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
*/
public Object nullSafeGet(ResultSet rs, String[] names, Object o)
throws HibernateException, SQLException {
Day day = (Day) o;
day.getCalendar().setTime((Date) Hibernate.DATE.nullSafeGet(rs, names[0]));
return day;
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
*/
public void nullSafeSet(PreparedStatement ps, Object o, int i)
throws HibernateException, SQLException {
Day day = (Day) o;
Hibernate.DATE.nullSafeSet(ps, day.getTime(), i);
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#deepCopy(java.lang.Object)
*/
public Object deepCopy(Object o) throws HibernateException {
if (o == null) return null;
Day origDay = (Day) o;
Day newDay = Day.today();
newDay.getCalendar().setTime(origDay.getTime());
return newDay;
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#isMutable()
*/
public boolean isMutable() {
return true;
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#disassemble(java.lang.Object)
*/
public Serializable disassemble(Object arg0) throws HibernateException {
return (Serializable) deepCopy(arg0);
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#assemble(java.io.Serializable, java.lang.Object)
*/
public Object assemble(Serializable arg0, Object arg1)
throws HibernateException {
return deepCopy(arg0);
}

/* (non-Javadoc)
* @see org.hibernate.usertype.UserType#replace(java.lang.Object, java.lang.Object, java.lang.Object)
*/
public Object replace(Object arg0, Object arg1, Object arg2)
throws HibernateException {
return deepCopy(arg0);
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 22, 2006 6:02 pm 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
You are confused. In nullSafeGet o is the owner - the containing entity - it's an account.

I don't know how your Day class is so I suppose that your code should be something like:
Code:
public Object nullSafeGet(ResultSet rs, String[] names, Object o)
throws HibernateException, SQLException {
   if(rs.wasNull())
      return null;
   Day day = new Day();
   day.getCalendar().setTime((Date) Hibernate.DATE.nullSafeGet(rs, names[0]));
   return day;
}


don't forget to rate


Last edited by pepelnm on Sat Jul 22, 2006 7:22 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 22, 2006 6:04 pm 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
Also, I'm not sure but I think you need to implement CompositeUserType in order to use your type in HQL queries.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 23, 2006 11:20 am 
Newbie

Joined: Sun Jun 04, 2006 7:02 am
Posts: 7
Location: Hoboken
pepelnm wrote:
You are confused. In nullSafeGet o is the owner - the containing entity - it's an account.

I don't know how your Day class is so I suppose that your code should be something like:
Code:
public Object nullSafeGet(ResultSet rs, String[] names, Object o)
throws HibernateException, SQLException {
   if(rs.wasNull())
      return null;
   Day day = new Day();
   day.getCalendar().setTime((Date) Hibernate.DATE.nullSafeGet(rs, names[0]));
   return day;
}


don't forget to rate


Thanks!!! That does it.
I was using the example on p. 152 of "Hibernate Quickly", so either I'm mis-reading it, or they didn't test it before printing it.
Cheers,
Ed


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

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.