java中的类转换异常

发布于 2024-12-09 16:38:14 字数 2938 浏览 0 评论 0原文

我正在休眠中编写查询,我的方法是:

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中查询“select groupid from b where nodeid in(devicelist)”,这里groupid的数据类型是int,nodeid是long

它给了我 Ljava.lang.Object 类转换异常;不能是 转换为 [Ljava.lang.Long;

我正在使用 PostgresSQl

请帮忙

Iam writing a query in hibernate and my method is:

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);
    }

Here the selecteddeviceId has values as [1234,12345, null, null]
and query from the table b "select groupid from b where nodeid in(devicelist)" here datatype of groupid is int and nodeid is long

It is giving me classcast exception as Ljava.lang.Object; cannot be
cast to [Ljava.lang.Long;

I am using PostgresSQl

Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青柠芒果 2024-12-16 16:38:14

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

如果您需要将已经是 Object[] 的 selecteddeviceID 分配给 Object[,为什么要将其转换为 Long[] ]?

尝试这样做

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

From your code: Object[] devArray = (Long[]) selecteddeviceID;

Why are you casting it to Long[] if you need to assign selecteddeviceID, which is already a Object[], to Object[]?

Try doing this instead

 Long[] devArray = Arrays.copyOf(selecteddeviceID, selecteddeviceID.length, Long[].class)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文