在Controller中具有空值的ArrayList

时间:2015-05-06 02:36:59

标签: java spring-mvc

我遇到了一个让我睡不着的问题。我的Controller中有一些ArrayList,但是当我执行我的方法时,它变为null,但在我的JPADAO中,值是正确的,我不知道该怎么做。

控制器

private List<Produto> produtos;
@Autowired
private ProdutoService produtoService;
public void produtoPorCategoria(Long id) throws ServiceException {
    setProdutos(produtoService.produtoPorCategoria(id));
}
//getters and setters

ProdutoService

@Override
public List<Produto> produtoPorCategoria(Long id) throws ServiceException {
    try {
        produtoDAO.produtoPorCategoria(id);
    } catch (DAOException ex) {
        Logger.getLogger(ProdutoServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

PRodutoJPADAO

@Override
public List<Produto> produtoPorCategoria(Long id) throws DAOException {
    TypedQuery<Produto> query = em.createNamedQuery("produto.produtoPorCategoria", Produto.class);
    query.setParameter("id", id);
    return query.getResultList();
}

我的查询

 @NamedQuery(name = "produto.produtoPorCategoria", query = "SELECT p FROM Produto p, Categoria c WHERE p.categoria.id  = c.id AND p.ativo = TRUE AND c.id = :id"),

当我调试并且值正确时,我的查询是正确的,JPADAO中的值也是正确的,但是当我执行setProdutos时,Arraylist变为null,我知道错误在控制器中,但是我无法弄清楚。

2 个答案:

答案 0 :(得分:0)

您在服务中的produtoPorCategoria方法结束时返回null。在dao call.produtoPorCategoria始终返回null,无论服务调用中是否完成任何操作。

答案 1 :(得分:0)

我做到了,并且工作

List<Produto> produtos = new ArrayList<>();
    try {

        produtos = produtoDAO.produtoPorCategoria(id);
    } catch (DAOException ex) {
        Logger.getLogger(ProdutoServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
    }
    return produtos;