Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

372
Vistas
Jackson: exclude every lazy collection on @Entity classes from serialization

In my Spring 4 project with Hibernate 5 and Java-based configuration I keep facing exception "could not initialize proxy - no Session " every time Jackson tries to serialize my entity with a lazy collection. It seems Jackson fails to check if collection is lazy and triggers loading which generates the exception. How do I make Jackson avoid serialization of every lazy-loaded collection on every @Entity-class and thus avoid constant exceptions and fails with "no Session"? The simplest working solution. I've read many approaches neighter of which really solves this problem for me. Any help will be appreciated (not for Spring Boot!). Some code snippet:

@Data
@Entity
@ToString(exclude="questions")
@Table(name = "theme")
public class Theme {

    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
    @Column(name = "id")
    private Long id;

    @Column(name = "title")
    private String title;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @OneToMany // LAZY by default
    @JoinColumn(name = "theme")
    private List<Question> questions;// = new ArrayList<>();
}

DAO

public interface ThemeDAO extends CrudRepository<Theme, Long> {
    List<Theme> findAll();
}

Exception goes here (in controller):

 ObjectMapper objectMapper = new ObjectMapper();
 result = objectMapper.writeValueAsString(theme);
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

jackson-datatype-hibernate add-on really solved the problem. I just added HibernateAwareObjectMapper as a separate class:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
public class HibernateAwareObjectMapper extends ObjectMapper {

    public HibernateAwareObjectMapper() {
            registerModule(new Hibernate5Module());
    }

}

And then overrode the method configureMessageConverters in MVC configurer class:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "learning_session.controller" })
public class WebContext extends WebMvcConfigurerAdapter implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext applicationContext) {
            this.applicationContext = applicationContext;
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            converters.add(new MappingJackson2HttpMessageConverter(new HibernateAwareObjectMapper()));
            super.configureMessageConverters(converters);
    }
// more beans 
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda