0

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

2
  • selectedDeviceID sounds like a single long to me, not an array. And you should change the initialize from object[] selectedDeviceID to long[] Commented Oct 14, 2011 at 8:08
  • As a general comment (and not addressing your immediate problem, so this is not an answer) I advise trying to put the logging and transaction management in aspects if you can, as that lets you boil that rather-long method down to something that's just doing “real” work. I find that makes code much easier to read and understand. Commented Oct 14, 2011 at 8:25

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

6 Comments

from the query nodeId is long so the devicelist ie the selecteddeviceId should also be long,, so i am trying to convert the object array to long
Also is it correct to convert to Object to long as did in the code??
java.lang.ArrayStoreException..i am getting this exception
Type of your value is not Long, you cannot store some other object to the Long array.
selectedeviceid coantains [1,2,3,null,null,null,null]..hw do i convert this into long??
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.