【问题标题】:Type cast issue for [Ljava.lang.Object[Ljava.lang.Object 的类型转换问题
【发布时间】:2013-03-20 17:01:37
【问题描述】:
  public List<Client> findClientByAssociateUser(String userId) {
        logger.info("Enter find list of clients by this user");
        org.hibernate.Query query = sessionFactory.getCurrentSession()
                .createQuery("SELECT c.id, c.clientName, c.billingAddress,c.contactNumber"
                + " from Client c, User ud"
                + " WHERE ud.id = c.userId and ud.id = :id")
                .setString("id", userId);
        List<Client> result = (List<Client>) query.list();
        logger.info("Exit find list of clients");
        return result;
    }

public ModelAndView userDetails(@PathVariable String id, HttpServletRequest request) {
    ModelAndView mvc = new ModelAndView();        
    List<Client> clientList = userRepository.findClientByAssociateUser(id.toString());
       mvc.addObject("clientList", clientList);
       for (Client client : clientList) {
        System.out.println("Client Name{" + client.getClientName());
    }
    mvc.setViewName(MANAGEUSER_PREFIX + "details");
    return mvc;
}

我得到:

Ljava.lang.Object; cannot be cast to Client

【问题讨论】:

  • o\你能显示你的堆栈跟踪吗

标签: spring hibernate jakarta-ee spring-mvc


【解决方案1】:

查询中的返回类型为List&lt;Object[] &gt;.

因为你的查询说

SELECT c.id, c.clientName, c.billingAddress,c.c......

改变

List<Client> result = (List<Client>) query.list();

然后按照那个处理

List<Object[]> result = (List<Object[]>) query.list();

或将查询更改为

   SELECT c from Client c......

【讨论】:

  • @Talha Bin Shakir 你看到我的解决方案了吗。试试这个会是问题
猜你喜欢
  • 1970-01-01
  • 2011-11-17
  • 2022-08-09
  • 2012-10-01
  • 2010-12-29
  • 1970-01-01
相关资源
最近更新 更多