Hibernate 6 getting null values from database for Json columns

81 Views Asked by At

We updated our spring boot application from spring boot 2 to 3 and we found out that in some cases when we request the entity with a json field in it we receive null value for the json field instead of the value from the database.

At the moment we are using:

  • java:17.0.2
  • spring-boot:3.1.5
  • hibernate:6.2.13
  • mysql:8.0.36

Our class is similar to this:

@Entity
@Table(name = "table_name")
public class Table {
... 
    @JdbcTypeCode(SqlTypes.JSON)
    @Column(name = "json_column_value")
    private JsonPOJO jsonColumnValue;

...

    public JsonPOJO getJsonPOJO() {
        return jsonColumnValue;
    }

    public void setJsonPOJO(JsonPOJO jsonColumnValue) {this.jsonColumnValue = jsonColumnValue; }
}

On persist the data is saved properly in the database but when we request it with the repository like tableRepository.findOneWithEagerRelationships(@Param("id") Long id) in the most cases it returns the Table entity properly with the json column but in some cases the json column value is null.

The repository looks something like this:

    @Query("select t FROM Table t " +
        "left join fetch t.manyLazyStuff  " +
        "where t.id = :id")
    Table findOneWithEagerRelationships(@Param("id") Long id);

Here all the lazy fields are fetched. (Json columns are not...)

Our Jackson configuration looks like this:

@Configuration
public class JacksonConfiguration {

    /**
     * Support for Hibernate types in Jackson.
     */
    @Bean
    public Hibernate6Module hibernate6Module() {
        return new Hibernate6Module();
    }

    /**
     * Jackson Afterburner module to speed up serialization/deserialization.
     */
    @Bean
    public AfterburnerModule afterburnerModule() {
        return new AfterburnerModule();
    }

    @Bean
    public HibernatePropertiesCustomizer jsonFormatMapperCustomizer(ObjectMapper objectMapper) {
        return properties -> properties
            .put(AvailableSettings.JSON_FORMAT_MAPPER, new JacksonJsonFormatMapper(objectMapper));
    }
}

Can anybody help us to figure out why we receive null values for this json columns?

Any suggestion is appreciated!

Update It seems that the issue is reproducible only with Mysql version 8.0.36 We were unable to find the reason why this happens but for now we use mysql v8.0.35

0

There are 0 best solutions below