-->
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.  [ 2 posts ] 
Author Message
 Post subject: Named query ClassCastException on Short param
PostPosted: Thu Jun 26, 2008 7:50 am 
Newbie

Joined: Thu Jun 26, 2008 7:34 am
Posts: 2
when we have a named query:

Code:
<query name="ThisIsAQuery">
    select * from artikel
    where artikel.artNr = :artNr
    and (:shortNr =-1 or artikel.shortNr = :shortNr)
</query>


With the mapping

Code:
<hibernate-mapping package="be.boss.component.artikel">
  <class name="Artikel" table="artikel">
    <id name="artNr" column="art_nr">
      <generator class="assigned"></generator>
    </id>
    <property name="shortNr" column="short_nr" type="java.lang.Short" />

</hibernate-mapping>


With the call:
Code:
  public List<ArtikelForPryzwyz> findByCriteriaForPryzwyz(Integer artNr,
      Short shortNr ) {
    return getHibernateTemplate().findByNamedQueryAndNamedParam(
        "ThisIsAQuery",
        new String[] { "artNr", "shortNr" },
        new Object[] { artNr, shortNr });
  }



We get the following exception:
Code:
java.lang.ClassCastException: java.lang.Short
   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.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:38)
   at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:491)
   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:2220)
   at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
   at org.hibernate.loader.Loader.list(Loader.java:2099)
   at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
   at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
   at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
   at org.springframework.orm.hibernate3.HibernateTemplate$34.doInHibernate(HibernateTemplate.java:1020)
   at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:419)
   at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
   at org.springframework.orm.hibernate3.HibernateTemplate.findByNamedQueryAndNamedParam(HibernateTemplate.java:1011)
   at be.boss.dao.artikel.hibernate.ArtikelDAOHibernateImpl.findByCriteriaForPryzwyz(ArtikelDAOHibernateImpl.java:294)
   at be.boss.dao.artikel.ArtikelDAOTest.testFindForPryzwyz(ArtikelDAOTest.java:302)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:585)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
   at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


Question: how to prevent this exception, when using Shorts? (When using Long's as parameters, we have to type -1L instead of -1 in the query)

Edit: hibernate version 3.2.3


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 07, 2008 10:09 am 
Newbie

Joined: Thu Aug 07, 2008 9:49 am
Posts: 1
Try using the cast function:

Code:
<query name="ThisIsAQuery">
    select * from artikel
    where artikel.artNr = :artNr
    and (:shortNr = cast(-1 as short) or artikel.shortNr = :shortNr)
</query>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.