java中的类转换异常
我正在休眠中编写查询,我的方法是:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的代码中:
Object[] devArray = (Long[]) selecteddeviceID;
如果您需要将已经是 Object[] 的 selecteddeviceID 分配给 Object[,为什么要将其转换为 Long[] ]?
尝试这样做
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