Type Exception Report
Message Could not write content: failed to lazily initialize a collection of role: br.com.casadocodigo.loja.models.Produto.precos, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->br.com.casadocodigo.loja.models.Produto["precos"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: br.com.casadocodigo.loja.models.Produto.precos, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->br.com.casadocodigo.loja.models.Produto["precos"])
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: failed to lazily initialize a collection of role: br.com.casadocodigo.loja.models.Produto.precos, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->br.com.casadocodigo.loja.models.Produto["precos"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: br.com.casadocodigo.loja.models.Produto.precos, could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->br.com.casadocodigo.loja.models.Produto["precos"])
package br.com.casadocodigo.loja.controllers;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import br.com.casadocodigo.loja.dao.ProdutoDAO;
import br.com.casadocodigo.loja.models.Produto;
@Controller
@RequestMapping("/relatorio-produtos")
public class RelatorioProdutosController {
@Autowired
ProdutoDAO produtoDao;
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public List<Produto> relatorioJson(@RequestParam(value= "dataLancamento", required = false) String dataLancamento) throws ParseException {
if(dataLancamento != null) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date dataFormatada = simpleDateFormat.parse(dataLancamento);
Calendar calendar = Calendar.getInstance();
calendar.setTime(dataFormatada);
return produtoDao.findComData(dataLancamento);
}
return produtoDao.findSemData();
}
}
classe ProdutoDAO:
public List<Produto> findComData(String dataLancamento) {
return manager.createQuery("select distinct(p) from Produto p join fetch p.precos precos where p.dataLancamento = :dataLancamento",
Produto.class).setParameter("dataLancamento", dataLancamento).getResultList();
}
public List<Produto> findSemData() {
return manager.createQuery("select p from Produto p", Produto.class).getResultList();
}