3 Replies Latest reply: Aug 16, 2012 3:59 PM by Amasoni RSS

    jpql multi selection query - java.lang.ClassCastException

    Amasoni
      Hello, i'm trying to get a Unit object from the query below.
      i am getting javax.ejb.EJBException in this method


      @NamedQuery(name = "getUnitIdUnitName", query = "SELECT u.unitId,u.unitName FROM Unit u "+ "ORDER BY u.unitName")

      public Unit getUnitsComboBox() {
      //get UnitId, UnitName from Units.
      Unit unitIDUnitName =(Unit) ((List<Object[]>)em.createNamedQuery("getUnitIdUnitName").getResultList()).get(0)[0];

      return unitIDUnitName;
      }

      Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to mmis.entity.Unit

      Thanx
        • 1. Re: jpql multi selection query - java.lang.ClassCastException
          gimbal2
          Amasoni wrote:
          Hello, i'm trying to get a Unit object from the query below.
          i am getting javax.ejb.EJBException in this method


          @NamedQuery(name = "getUnitIdUnitName", query = "SELECT u.unitId,u.unitName FROM Unit u "+ "ORDER BY u.unitName")

          public Unit getUnitsComboBox() {
          //get UnitId, UnitName from Units.
          Unit unitIDUnitName =(Unit) ((List<Object[]>)em.createNamedQuery("getUnitIdUnitName").getResultList()).get(0)[0];

          return unitIDUnitName;
          }

          Caused by: java.lang.ClassCastException: java.lang.Long cannot be cast to mmis.entity.Unit

          Thanx
          You don't want to select the columns, you want to select the object itself.

          SELECT u FROM Unit u ORDER BY u.unitName

          or even shorter:

          FROM Unit u ORDER BY u.unitName


          Try to reason where to put your question by the way, it is not about Javaserver Faces. If you can't find a forum that clearly matches consider putting it in Java Programming. In this case it was entirely unnecessary since you already posted JPA questions in the EJB forum, which is a decent match.
          • 2. Re: jpql multi selection query - java.lang.ClassCastException
            Amasoni
            The reason i'm selecting the columns is becuase there are other fields in Unit.
            • 3. Re: jpql multi selection query - java.lang.ClassCastException
              Amasoni
              I sorted out my issue using the method below.

              public ArrayList<Unit> getUnitsComboBox() {
              //get UnitId, UnitName from Units.
              System.out.println( " Unit Name: before " );
              ArrayList<Unit> arrUnit=new ArrayList();

              List<Object[]> qry=em.createNamedQuery("getUnitIdUnitName").getResultList();
              int i=0;
              for(Object[] obj: qry){
              Long unitId =(Long) ((List<Object[]>)em.createNamedQuery("getUnitIdUnitName").getResultList()).get(i)[0];
              String unitName =(String) ((List<Object[]>)em.createNamedQuery("getUnitIdUnitName").getResultList()).get(i)[1];
              //System.out.println( " Unit id: "+unitId );
              //System.out.println( " Unit Name: "+unitName );
              Unit combo=new Unit(unitId,unitName);
              arrUnit.add(combo);
              i++;
              }
              return arrUnit;
              }