java - Controller 中具有空值的ArrayList

标签 java spring-mvc

我遇到了一个影响我 sleep 的问题。我的 Controller 中有一些 ArrayList,但是当我执行我的方法时,它变成 null,但在我的 JPADAO 中,值是正确的,我不知道该怎么办了。

Controller

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

产品服务

@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变为空,我知道错误在 Controller 中,但我无法弄清楚。

最佳答案

您将在服务中的 produtoPorCategoria 方法末尾返回 null。在 dao 调用之后。无论服务调用中执行什么操作,produtoPorCategoria 始终返回 null。

关于java - Controller 中具有空值的ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30066498/

相关文章:

java - 如何在 <form :select>? 中使用占位符属性

java - Servlet 文件上传 ismultipartcontent 返回 false

java - @ModelAttribute在Spring框架中如何工作?

java - 如何使用 egit 插件在 Eclipse 中启用提交更改

java - 未调用 httpAsyncClient.execute() 回调方法

java - 时间戳最后带有.0

java - 如何通过springboot使用post方法获取静态html页面

java - Spring Security SAML 实现

java - 快速排序。线程中出现异常 "main"java.lang.StackOverflowError

java - 如何在点击图像时生成事件?