java中的ClassCastException

时间:2011-10-14 08:06:48

标签: java hibernate postgresql

我在hibernate中编写一个查询,我的方法是:

public String a(Object[] selectedAlarmId,
            Object[] selecteddeviceID, String loggedInUser, String clientIp,
            String role) throws Exception {
        int result = 0;
        b grp = new b();
        try {

            Session hiberSession = HibernateSessionFactory.currentSession();
            Transaction transaction = hiberSession.beginTransaction();

            List deviceQueryList = null;

            String clauseAppender = ("select max(severity),devicenodeid  from AlarmMappingBean where devicenodeid in (:devicelist) group by devicenodeid");
            Query deviceQuery = hiberSession.createQuery(clauseAppender);
            deviceQuery.setParameterList("devicelist", selecteddeviceID);
            deviceQueryList = deviceQuery.list();
            Iterator<Object[]> iter = deviceQueryList.iterator();

            while(iter.hasNext()){              
                Object[] objAlarm = iter.next();
                System.out.println(objAlarm.length);
                System.out.println("Severity - > " + objAlarm[0]);
                System.out.println("Device Node ID - > " + objAlarm[1]);            
                Query updateMangedNode = hiberSession
                .createQuery("update ManagedNode  set highestSeverity =? where nodeId = ?");
                updateMangedNode.setParameter(0, objAlarm[0]);
                updateMangedNode.setParameter(1, Long.parseLong(objAlarm[1].toString()));               
                //updateMangedNode.executeUpdate();
            }
        //  Long[] deviceArray =(Long[]) selecteddeviceID; 
        Exception occurs here-->    Object[] devArray = (Long[]) selecteddeviceID;


            Query groupQuery = hiberSession
                    .createQuery("select groupId from b where nodeId in (:devicelist)");

              groupQuery.setParameterList("devicelist", devArray);

            List<ManagedNode> devicelist = new ArrayList<ManagedNode>();
            devicelist = groupQuery.list();
            if(!(devicelist.isEmpty() )){
            Iterator<ManagedNode> itergroup =devicelist.iterator(); 
            while(itergroup.hasNext()){
                ManagedNode objgroup = itergroup.next();                
                grp.updateGroupHighestSeverity(objgroup.getGroupId());
            }


            }
            transaction.commit();


        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            HibernateSessionFactory.closeSession();
        }
        return Integer.toString(result);
    }

这里selecteddeviceId的值为[1234,12345,null,null] 并从表b中查询“从b中选择groupid,其中nodeid in(devicelist)”这里groupid的数据类型为int,nodeid为long

  

它给了我一个classcast异常作为Ljava.lang.Object;不可能是   施放到[Ljava.lang.Long;

我正在使用PostgresSQl

请帮忙

1 个答案:

答案 0 :(得分:1)

从您的代码:Object[] devArray = (Long[]) selecteddeviceID;

如果需要将selecteddeviceID(已经是Object [])分配给Object [],为什么要将它转换为Long []?

尝试这样做

 Long[] devArray = Arrays.copyOf(selecteddeviceID, selecteddeviceID.length, Long[].class)