'Spring Hibernate Jackson Hibernate5Module

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 ...



Solution 1:[1]

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();
}

Solution 2:[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()));

    }

}

Solution 3:[3]

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));

}

Solution 4:[4]

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;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michael
Solution 2 Pascal
Solution 3 NKonsul
Solution 4 Az.MaYo