Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-configure Hibernate with a JsonFormatMapper that uses the auto-configured Jackson ObjectMapper #33870

Open
BernhardPosselt opened this issue Jan 18, 2023 · 7 comments
Labels
status: blocked An issue that's blocked on an external project change status: pending-design-work Needs design work before any code can be developed type: enhancement A general enhancement
Milestone

Comments

@BernhardPosselt
Copy link

BernhardPosselt commented Jan 18, 2023

After migrating to Spring Boot 3.0.1 and Hibernate 6, I swapped out the Hibernate Types JSON support for the new built in @JdbcTypeCode(SqlTypes.JSON), however my Kotlin data classes failed to deserialize and I had to annotate everything with @JsonCreator and @JsonProperty to fix constructors.

I feel like this should default to the Jackson instance on the Spring context rather than the Jackson library itself, that is pulled in by Hibernate by default. It should be possible to define a custom mapper factory that can be configured via hibernate.type.json_format_mapper and point Hibernate to that instance, see https://github.com/hibernate/hibernate-orm/blob/main/documentation/src/main/asciidoc/userguide/chapters/domain/basic_types.adoc#json-mapping

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 18, 2023
@wilkinsona
Copy link
Member

Thanks for the suggestion. If I have undertood it correctly, you should be able to do this yourself using a HibernatePropertiesCustomizer bean:

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

Can you please try this and let us know if it allows you to remove the @JsonCreator and @JsonProperty annotations?

@BernhardPosselt
Copy link
Author

BernhardPosselt commented Jan 18, 2023

Thank you for getting back, can confirm that this does in fact work fine after removing the annotations. Maybe warrants a separate Kotlin compatibility section in the docs unless it's done by default?

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jan 18, 2023
@wilkinsona wilkinsona changed the title Kotlin Jackson Mapper not used when on classpath when serializing/deserializing @JdbcTypeCode(SqlTypes.JSON) using Spring Data JPA Auto-configure Hibernate with a JsonFormatMapper that uses the auto-configured Jackson ObjectMapper Jan 18, 2023
@wilkinsona wilkinsona added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Jan 18, 2023
@wilkinsona wilkinsona added this to the 3.x milestone Jan 18, 2023
@wilkinsona
Copy link
Member

wilkinsona commented Jan 18, 2023

Thanks for confirming that it works for you. We should consider making this the default behaviour so I've labelled this issue as enhancement.

When we come to implement this we should consider the effect that Hibernate using the auto-configured ObjectMapper may have on an application. It could be a breaking change so we may want a way to disable the behavior. We should also try to implement it in such a way that if the user is already customizing the JSON format mapper that we do not overwrite that customization.

@davidfortin55555
Copy link

davidfortin55555 commented Jul 4, 2023

I have implemented the following code but it is nor working with springboot 3.0.1 and hibernate 6

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

Do you habe another proposal?

@wilkinsona
Copy link
Member

It should work so, without knowing exactly how it isn't working, it's difficult to propose something else. If you'd like some help, please post a question on Stack Overflow that includes a minimal, reproducible example of the problem.

@wilkinsona
Copy link
Member

While looking at #42676 and trying to find some inspiration for a property name, a couple of other things came to mind:

  1. We should consider auto-configuring a JsonBJsonFormatMapper when there's a Jsonb bean. This also raises a question about what to do when there's both an ObjectMapper bean and a Jsonb bean. We've got spring.mvc.converters.preferred-json-mapper but its name clearly indicates that it shouldn't apply outside of Spring MVC's message conversion.
  2. The functionality is marked as @Incubating in Hibernate. Are we jumping the gun a bit here and would we be better waiting until its graduated from incubation before we try to offer auto-configuration.

@wilkinsona wilkinsona added status: pending-design-work Needs design work before any code can be developed for: team-meeting An issue we'd like to discuss as a team to make progress labels Oct 18, 2024
@philwebb
Copy link
Member

philwebb commented Jan 6, 2025

We discussed this today and we'd like to wait until the Hibernate feature is not @Incubating.

@philwebb philwebb added status: blocked An issue that's blocked on an external project change and removed for: team-meeting An issue we'd like to discuss as a team to make progress labels Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: blocked An issue that's blocked on an external project change status: pending-design-work Needs design work before any code can be developed type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

5 participants