Announcement Announcement Module
Collapse
No announcement yet.
Cast Exception Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cast Exception

    getting java.lang.CastException

    for(Iterator it=list.iterator();it.hasNext(){
    //***Getting exception at this line
    TestPayroll payroll = (TestPayroll) it.next();
    }

    Method below gets TestPayroll
    public List getRecords(te....,....){
    List list = (List)this.getHibernateTemplate().executeFind(
    new HibernateCallback(){
    public Object doInHibernate(Session session) throws
    HibernateException, SQLException{
    try{
    Query query = session.createQuery("select distinct
    payrollPK.accountId as accountID , payrollPK.empId as empId," +
    "(select sum(e2.amount) from ErsxpPayroll e2 where e2.payrollPK.accountId = e1.payrollPK.accountId) as amount " +
    "from TestPayroll e1 " +
    "where client_code=:clientCode " +
    "and rpcode=:rpCode " +
    "and begdate >= :begDate " +
    "and enddate <= :endDate " +
    "and empid in (:empIdList) " +
    "group by e1.payrollPK.accountId, e1.payrollPK.empId,e1.amount "
    //,payrollPK.clientCode, begDate, payrollPK.endDate,rpCode,rpType,postDate
    )


    return query.List

  • #2
    [ code][/code ] tags are there for a reason, please use them!

    Posting your stacktrace might help.

    Also your query isn't returing those objects it is returing an List of Object[] at least that is what your select clause tells me.

    Comment


    • #3
      Cast Exception

      Query returning list of objects, I am iterating through theses objects.

      When I do query as session.createQuery("from TestPayroll ........) it works fine, but when I use select with column name giving java.Lang.CastException.

      Thank you for your time.

      Comment


      • #4
        I suggest you read the hibernate reference documentation.

        Code:
        from TestPayRoll
        The above query will indeed return TestPayroll objects. however as soons as you determine a specific select clause, as in your case. There are NO TestPayroll objects anymore. It becomes an Object[] of the values NOT testPayroll objects.

        Code:
        "select distinct
        payrollPK.accountId as accountID , payrollPK.empId as empId," +
        "(select sum(e2.amount) from ErsxpPayroll e2 where e2.payrollPK.accountId = e1.payrollPK.accountId) as amount " +
        "from TestPayroll e1 " +
        "where client_code=:clientCode " +
        "and rpcode=:rpCode " +
        "and begdate >= :begDate " +
        "and enddate <= :endDate " +
        "and empid in (:empIdList) " +
        "group by e1.payrollPK.accountId, e1.payrollPK.empId,e1.amount "
        //,payrollPK.clientCode, begDate, payrollPK.endDate,rpCode,rpType,postDate
        As I stated before, this query will result in a List of Object[] NOT a List of TestPayroll objects!

        Comment


        • #5
          Cast Exception

          Can you please tell me how to get the data from List of Objects[].

          Thank you.

          Comment


          • #6
            I suggest you read the hibernate reference guide, it is explained in there. Also an Object[] is no different then a TestPayroll object. Only a different cast.

            Code:
            for(Iterator it=list.iterator();it.hasNext(){
            //***Getting exception at this line
              Object[] results = (Object[]) it.next();
            }

            Comment


            • #7
              Fixed

              Thank you Mr. Marten,

              I fixed it with your help. I thought getting Testpayroll, but I was getting data.

              for(Iterator it=list.iterator();it.hasNext(){
              //***Getting exception at this line
              Object[] results = (Object[]) it.next();
              results[0]
              }

              Comment

              Working...
              X