gpt4 book ai didi

java - java中的ClassCastException

转载 作者:行者123 更新时间:2023-12-01 15:42:43 27 4
gpt4 key购买 nike

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

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

我正在使用PostgresSQl

请帮忙

最佳答案

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

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

尝试这样做

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

关于java - java中的ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7764769/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com