Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Problemas com cache no rest

Com uso do cache no método abaixo:

    public List<Livro> ultimosLancamentos() {
        String jpql = "Select l From Livro l join fetch l.autores order by l.id desc";
        return manager.createQuery(jpql, Livro.class).setMaxResults(5).setHint(QueryHints.HINT_CACHEABLE, true).getResultList();
    }

Ao utiliza-lo na primeira vez, ocorre tudo bem, mas eu repetir a requisição via GET, apresenta a exceção:

Caused by: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: br.com.casadocodigo.loja.model.Livro.autores, could not initialize proxy - no Session
    at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:587)
    at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:204)
    at org.hibernate.collection.internal.AbstractPersistentCollection.readSize(AbstractPersistentCollection.java:148)
    at org.hibernate.collection.internal.PersistentBag.size(PersistentBag.java:261)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:102)
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:672)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678)
    ... 61 more
1 resposta
solução!

Retirei o Stateful e funcionou.

package br.com.casadocodigo.loja.daos;

import java.util.List;

import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import org.hibernate.jpa.QueryHints;

import br.com.casadocodigo.loja.model.Livro;

//@Stateful
public class LivroDao {

    @PersistenceContext//(type = PersistenceContextType.EXTENDED)
    private EntityManager manager;

    public void salvar(Livro livro) {
        manager.persist(livro);
    }

    public List<Livro> listar() {
        String jpql = "Select distinct(l) from Livro l join fetch l.autores";

        return manager.createQuery(jpql, Livro.class).getResultList();
    }

    public List<Livro> ultimosLancamentos() {
        String jpql = "Select l From Livro l join fetch l.autores order by l.id desc";
        return manager.createQuery(jpql, Livro.class).setMaxResults(5).getResultList();
    }

    public List<Livro> demaisLivros() {
        String jpql = "Select l From Livro l order by l.id desc";
        return manager.createQuery(jpql, Livro.class).setFirstResult(5).setHint(QueryHints.HINT_CACHEABLE, true).getResultList();
    }

    public Livro buscarPorId(Integer id) {
        //return manager.find(Livro.class, id);
        StringBuilder jpql = new StringBuilder();
        jpql.append("Select l From Livro l");
        jpql.append(" join fetch l.autores ");
        jpql.append(" where l.id = :id");
        return manager.createQuery(jpql.toString(), Livro.class).setParameter("id", id).getSingleResult();
    }
}

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software