Hi All.
I've encountered a weird bug with Criteria's (I'm assuming) using hibernate3.
Essentially, it seems that somewhere within Hibernate integers are being misrepresented as Strings.
In the code below, sc is my SearchCriteria class, which allows for a search parameters to be specified generically. SearchCriteria holds a columnName and place holders for Integer, String and Date objects. In the below case I'm searching on an Integer value
Code:
if (sc.getIntegerValue() != null)
{
Integer vals [] = new Integer[1];
vals[0] = sc.getIntegerValue();
e = Expression.eq(sc.getFieldName(), sc.getIntegerValue());
}
When evaluated this generates a Criteria object with the string value CriteriaImpl(com.incognitointeractive.api.impl.AccountImpl:this[][Status=20])
Performing this search results in a Class Cast Exception. Stack trace is below:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at org.hibernate.type.IntegerType.set(IntegerType.java:41)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:136)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:116)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1707)
at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1678)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1563)
at org.hibernate.loader.Loader.doQuery(Loader.java:673)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2213)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
However oddly, the below code functions correctly
Code:
if (sc.getIntegerValue() != null)
{
Integer vals [] = new Integer[1];
vals[0] = sc.getIntegerValue();
ep = Expression.in(sc.getFieldName(), (Object []) vals);
}
resulting in a Criteria of CriteriaImpl(com.incognitointeractive.api.impl.AccountImpl:this[][Status in (20)])
and the correct results returned.
I'm 99% certain that the first approach worked as expected until recently - while the second approach works, it has led to some ugly code which I'd prefer the eliminate. Mapping file below.
Code:
<?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>
<class name="com.incognitointeractive.api.impl.AccountImpl" table="ACCOUNT" >
<id name="Id" type="java.math.BigDecimal" column="ACCOUNTID" >
<generator class="increment" />
</id>
<property name="AccountNumber" type="java.lang.String" column="ACCOUNTNUMBER" length="42" />
<property name="Status" type="java.lang.Integer" column="ACCOUNTSTATUSID"/>
<property name="ServiceProvider" type="java.lang.Integer" column="SERVICEPROVIDERID"/>
<property name="StartDate" type="java.util.Date" column="STARTDATE"/>
<property name="EndDate" type="java.util.Date" column="ENDDATE"/>
</class>
</hibernate-mapping>
Hopefully, someone else has run into this.
Any suggestions welcomed.
Thanks,
Sean