4

I have set up spring 4.3.1 with Hibernate 5.1.0 and Jackson 2.7.5

I had some lazy init Exceptions because the Jackson ObjectMapper tries to convert my Objects to late when I am out of the Transactional Service.

Therefore I have read the Hibernate5Module for Jackson.

After adding the Module I do not get lazy Exceptions BUT all @JsonView Annotations are ignored and my lazy collections are 'null'

public class SpringWebConfig extends WebMvcConfigurerAdapter {

    ...


    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {

        for (HttpMessageConverter<?> converter : converters) {
            if (converter instanceof org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) {
                ObjectMapper mapper = ((MappingJackson2HttpMessageConverter) converter).getObjectMapper();
                mapper.registerModule(new Hibernate5Module());
            }
        }

    }

}

Is there anything I am doing wrong? The Hibernate5Module should initialize the lazy collections ...

Pascal
  • 2,059
  • 3
  • 31
  • 52

4 Answers4

4

By creating your own ObjectMapper, you're overriding the one Spring Boot would set up, which would include a bunch of useful modules, such as Jdk8 module.

What you should do instead, is just add the Hibernate5() module to the Application Context and Spring Boot will automatically add it to the ObjectMapper that it sets up. Like this in any @Configuration class:

@Bean
public Hibernate5Module hibernate5Module() {
    return new Hibernate5Module();
}
Michael
  • 556
  • 3
  • 12
  • There are couple of default configurations which may not fit for everyone. for example by default @Transiant will be ignored. That's why I added detailed answer. – Az.MaYo May 06 '22 at 07:35
  • I checked on this suggestion I am getting null like lazilyFetchedList : null – Pradeep Virtuele Aug 25 '22 at 04:09
2

Got it to work with the following

@EnableWebMvc
@Configuration
@ComponentScan({ "..." })
public class SpringWebConfig extends WebMvcConfigurerAdapter {

    @Autowired
    SessionFactory sf;

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

        Hibernate5Module module = new Hibernate5Module(sf);
        module.disable(Feature.USE_TRANSIENT_ANNOTATION);
        module.enable(Feature.FORCE_LAZY_LOADING);

        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        builder.modulesToInstall(module);

        converters.add(new MappingJackson2HttpMessageConverter(builder.build()));

    }

}
Pascal
  • 2,059
  • 3
  • 31
  • 52
1

I manage to make it work with the below implementation

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

    Hibernate5Module module = new Hibernate5Module(); // or Hibernate4Module ... depends on hibernate version you are using

    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(module);

    converters.add(new MappingJackson2HttpMessageConverter(mapper));

}
NKonsul
  • 25
  • 4
Ponleu
  • 1,492
  • 12
  • 27
0

jackson-datatype-hibernate5 bring many solutions but there are some default configurations as well.

Please have a look on

Below is the configuration I did as per my project requirements.

@Bean
public Hibernate5Module hibernateModule() {
    Hibernate5Module module = new Hibernate5Module();
    module.disable(Hibernate5Module.Feature.USE_TRANSIENT_ANNOTATION);
    module.enable(Hibernate5Module.Feature.FORCE_LAZY_LOADING);
    return module;
}
Az.MaYo
  • 1,044
  • 10
  • 23