Java.Lang.Object,无法转换为Backed.Bean

时间:2016-04-06 06:32:27

标签: java classcastexception

我正在创建一个本机查询并获​​取结果列表,我想在标签中显示但得到错误

private void requestsButtonClicked() {
  try {
    String l_query = "";
    l_query = "SELECT * from tablename";
    Controller controller = new Controller(ui.getDbConnection());
    List<RequestBean> l_list = l_ric.getNativeQueryResult(l_query, RequestBean.class);
    m_requestResultNumber.setCaption(l_list.size() + "");
  } catch (Exception e) {
    m_logger.error("CATCH", e);
    org.ids.geoais.common.Process.showErrorMessage(m_RefGeoAISui, e);
  }
}

这是本机查询的方法

public <T> List<T> getNativeQueryResult(String p_query, Class<T> p_class) throws Exception {
  m_logger.debug("BaseController::getNativeQueryResult()");
  // TODO Auto-generated method stub
  m_logger.debug("SQL = " + p_query);
  List<T> l_returnList = new ArrayList<T>();
  EntityManager l_em = m_connectionInstance.getEntityManager();
  Query l_query = l_em.createNativeQuery(p_query);
  for (Object l_obj : l_query.getResultList()) {
    if (null != l_obj) {
      if (p_class.isAssignableFrom(l_obj.getClass())) {
        l_returnList.add(p_class.cast(l_obj));
      } else {
        m_logger.warn("Cannot cast the object of type " + l_obj.getClass().getName() + " to object of type " + p_class.getName());
      }
    } else {
      m_logger.warn("Search object in get query result is NULL");
    }
  }
  return l_returnList;
}
  

错误:[2016-04-06 11:51:38,406] - WARN - (db.BaseController) - 不能   强制转换[Ljava.lang.Object;到类型的对象   backend.bean [2016-04-06 11:51:38,406] - WARN - (db.BaseController) -   无法强制转换类型为[Ljava.lang.Object;到类型的对象   backend.bean [2016-04-06 11:51:38,406] - WARN - (db.BaseController) -   无法强制转换类型为[Ljava.lang.Object;到类型的对象   backend.bean

1 个答案:

答案 0 :(得分:2)

您似乎在此调用中省略了实体类型:

Query l_query = l_em.createNativeQuery(p_query);

尝试

Query l_query = l_em.createNativeQuery(p_query, p_class);