-->
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.  [ 6 posts ] 
Author Message
 Post subject: classcastException
PostPosted: Sat Apr 08, 2006 10:24 pm 
Newbie

Joined: Sat Apr 08, 2006 10:20 pm
Posts: 3
i always get a classcastexception when i am trying to map oracle's emp table
this is my mapping file
<hibernate-mapping>
<class name="org.hibernate.anu.Emp" table="EMP" schema="SCOTT">
<id name="empno" type="long">
<column name="EMPNO" precision="4" scale="0" />
<generator class="assigned" />
</id>

<property name="deptno" type="int">
<column name="DEPTNO" length="2"/>
</property>
<property name="ename" type="string">
<column name="ENAME" length="10" />
</property>
<property name="job" type="string">
<column name="JOB" length="9" />
</property>
<property name="mgr" type="long">
<column name="MGR" precision="4" scale="0" />
</property>
<property name="hiredate" type="timestamp">
<column name="HIREDATE" length="7" />
</property>
<property name="sal" type="double">
<column name="SAL" precision="7" />
</property>
<property name="comm" type="double">
<column name="COMM" precision="7" />
</property>
</class>
</hibernate-mapping>
The pojo class is

private long empno;
private int deptno;
private String ename;
private String job;
private long mgr;
private Date hiredate;
private double sal;
private double comm;


// Constructors

/** default constructor */
public Emp() {
}

/** minimal constructor */
public Emp(long empno) {
this.empno = empno;
}

/** full constructor */
public Emp(long empno, int deptno, String ename, String job, long mgr, Date hiredate,double sal, double comm) {
this.empno = empno;
this.deptno = deptno;
this.ename = ename;
this.job = job;
this.mgr = mgr;
this.hiredate = hiredate;
this.sal = sal;
this.comm = comm;
}


// Property accessors

public long getEmpno() {
return this.empno;
}

public void setEmpno(long empno) {
this.empno = empno;
}

public int getDeptno() {
return this.deptno;
}

public void setDeptno(int deptno) {
this.deptno = deptno;
}

public String getEname() {
return this.ename;
}

public void setEname(String ename) {
this.ename = ename;
}

public String getJob() {
return this.job;
}

public void setJob(String job) {
this.job = job;
}

public long getMgr() {
return this.mgr;
}

public void setMgr(long mgr) {
this.mgr = mgr;
}

public Date getHiredate() {
return this.hiredate;
}

public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}

public double getSal() {
return this.sal;
}

public void setSal(double sal) {
this.sal = sal;
}

public double getComm() {
return this.comm;
}

public void setComm(double comm) {
this.comm = comm;
}


stack trace is

Hibernate: select this_.EMPNO as EMPNO0_, this_.DEPTNO as DEPTNO0_0_, this_.ENAME as ENAME0_0_, this_.JOB as JOB0_0_, this_.MGR as MGR0_0_, this_.HIREDATE as HIREDATE0_0_, this_.SAL as SAL0_0_, this_.COMM as COMM0_0_ from SCOTT.EMP this_ where this_.SAL>=?
java.lang.ClassCastException: java.lang.String
at org.hibernate.type.DoubleType.set(DoubleType.java:34)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:62)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:44)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1115)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1177)
at org.hibernate.loader.Loader.doQuery(Loader.java:390)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
at org.hibernate.loader.Loader.doList(Loader.java:1593)
at org.hibernate.loader.Loader.list(Loader.java:1577)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:111)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1322)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:300)
at org.hibernate.anu.criteriaexample.main(criteriaexample.java:18)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 1:39 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You didn't post your code, so I can't point out the exact location of the error, but your problem is that you're passing a String object into a criteria Restriction on one of the double fields. You're probably grabbing the string from the user and putting it directly into the criteria: you have to convert it to a double first.

When posting code, or anything that will look better if it's monospaced, please use code tags. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 1:38 pm 
Newbie

Joined: Sat Apr 08, 2006 10:20 pm
Posts: 3
I am attaching the code...



Code:
public static void main(String[] args) {
      Session session=null;
      try{
         SessionFactory sessionfactory=new Configuration().configure().buildSessionFactory();
         session=sessionfactory.openSession();
         Criteria crit=session.createCriteria(Emp.class);
         crit.add(Restrictions.ge("sal","1000"));
         List lt=crit.list();
         for(Iterator it=lt.iterator();it.hasNext();)
         {
            Emp emp=(Emp)it.next();
            System.out.println(emp.getEname());
            System.out.println(emp.getEmpno());
            System.out.println(emp.getSal());
            System.out.println(emp.getDeptno());
            System.out.println(emp.getHiredate());
         }
         session.close();
         
      }catch(ClassCastException e){
         e.printStackTrace();
         
      }finally{ }
   }
   

}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 9:48 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I was right then. Can you see the bug in this line?
Code:
crit.add(Restrictions.ge("sal","1000"));
You have defined, in your mapping file, that sal is a double. But you're passing a string in. You have to change that to a double.
Code:
crit.add(Restrictions.ge("sal", 1000.0));


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 10:56 pm 
Newbie

Joined: Sat Apr 08, 2006 10:20 pm
Posts: 3
Still not working!!!
crit.add(Restrictions.ge("sal",1000.0));
Shows a sysntax error if i execute this, Any help would be highly appreciated..thanx


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 10, 2006 11:09 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You're obviously not using Java5? Change it to
Code:
crit.add(Restrictions.ge("sal", new Double(1000.0)));
And feel free to feel embarassed about not being able to fix that on your own.


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