0

I'm having a problem with OpenSessionInViewFilter and objects described in Sections, to do in a given request, occurs:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: ... , could not initialize proxy - no Session

My Controller:

@Controller
public class ProdutoIntercambiavelController {

@Autowired
private IProdutoService produtoService;

private Produto getProduto(HttpSession session){
    return (Produto)session.getAttribute("produto");
}

private void setProduto(Produto produto, HttpSession session){
    session.setAttribute("produto", produto);
}

@RequestMapping(value="/Produto/Editar/{id}/Intercambiavel/", method= RequestMethod.GET)
public String index(@PathVariable int id,Model model, HttpSession session){
    if (getProduto(session)==null)
        setProduto(produtoService.Abrir(id), session);

    model.addAttribute("produto", getProduto(session));

    return "editintercambiavel";
}

My Entity:

@Entity
public class Produto implements Serializable {

        ...

    @OneToMany(fetch=FetchType.LAZY)
    private List<ItemKitProduto> itensKitProduto = new ArrayList<ItemKitProduto>();

    @OneToMany(mappedBy="produto_pai", fetch=FetchType.LAZY)
    private List<Produto> intercambiaveis = new ArrayList<Produto>();
    @OneToOne
    private Produto produto_pai;
}

what would be the best practice to handle entities in session?

JulioCes
  • 9
  • 6
  • Don't put them in the session. Your code is also dangerous as it will retrieve only once after that it is in the session, next product with other id will not be retrieved. However if you really must you need to reattach it to a session (if you want to be able to use it over multiple request/page views). You could write a Filter or HandlerInterceptor to do that for you. – M. Deinum May 15 '14 at 12:01
  • One question, my session should not close after I finish loading the JSP. Ate while processing my request, the error occurs. – JulioCes May 15 '14 at 15:35
  • Yes it should, the session closes right after it finished rendering. The session is only maintained until after the rendering of the jsp. – M. Deinum May 16 '14 at 05:38
  • Thanks, I used another way to manipulate that data. To edit values ​​using request, to add new data usage session. – JulioCes May 26 '14 at 19:00

0 Answers0