I am learning Spring Boot and actually I am trying to fetch lazy loaded associations just when it is needed and it does not include the Json generated during the page rendering.
I have a class Person which have an auto-association "father":
@Entity(name = "Person")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long idPerson;
private String Name;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "idFather")
private Person father;
/*Getters and setters ommited*/
}
I have configured an object mapper with jackson as suggested by this site:
@Bean
public Jackson2ObjectMapperBuilder configureObjectMapper() {
return new Jackson2ObjectMapperBuilder()
.modulesToInstall(Hibernate5Module.class);
}
Finally I have a GET endpoint that will return a single Person if the id is informed or list everyone otherwise. My PersonRepository simple extends the CrudRepository interface.
When I try to fetch a single record the father is not loaded as expected:
//localhost:9000/person/2
{"idPerson":2,"name":"Luke Skywalker","father":null}
But if I try to retrieve all People (CrudRepository.listAll) the associations are fetched:
//localhost:9000/person
[
{
"idPerson":1,
"name":"Darth Vader",
"father":null
},
{
"idPerson":2,
"name":"Luke Skywalker",
"father":{
"idPerson":1,
"name":"Darth Vader",
"father":null
}
}
]
I do not desire this behavior and I am probably missing some configuration on the Object Mapper.
Does someone have an idea about what I should do?
Edit:
I dont think this is a duplication to Avoid Jackson serialization on non fetched lazy objects.
First: The answer provided there is old and WebMvcConfigurerAdapter is deprecated on Hibernate 5.
Second: Based on Configure Jackson to omit lazy-loading attributes in Spring Boot. I suspect the solution provided by @r1ckr on the first topic is actually equivalent to what I am using:
@Bean
public Jackson2ObjectMapperBuilder configureObjectMapper() {
return new Jackson2ObjectMapperBuilder()
.modulesToInstall(Hibernate5Module.class);
}
Third: Even knowing abot the deprecation I tried the solution proposed with the same behavior which reinforced the second point.
Fourth: I tried the same approach using the actual interface WebMvcConfigurer and again achieved the same behavior.
@Configuration
@EnableWebMvc
public class Configuration implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(jacksonMessageConverter());
WebMvcConfigurer.super.configureMessageConverters(converters);
}
public MappingJackson2HttpMessageConverter jacksonMessageConverter() {
MappingJackson2HttpMessageConverter messageConverter =
new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Hibernate5Module());
messageConverter.setObjectMapper(mapper);
return messageConverter;
}
}