Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

364
Views
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);
about 4 years ago · Santiago Trujillo
1 answers
Answer question

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 
}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!