从 Object 转换为 Class :失败

cast from Object to Class : failed

我收到这个错误

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to xxx.xxx.xxx.Terminal

当我尝试这段代码时;那怎么了??

for(int k=0;k<argTerminal.length;k++){         
String hql = "select crimpkontakt from Terminal where id="+argTerminal[k];
Query query = session.createQuery(hql);
 query.setMaxResults(1);
 Terminal nameTerminal = (Terminal) query.uniqueResult();

如果您想 select 终端对象,您的 HQL 应如下所示:

String hql = "from Terminal where id="+argTerminal[k]

另外为了避免SQL注入,使用参数API是首选:

Query query = session.createQuery("select from Terminal where id = :id");
query.setString("id", argTerminal[k]); // I assume that id is a String here
query.setMaxResults(1);

如果 id 是主键则更好,您可以使用 session.get(Terminal.class, argTerminal[k])