0

I need to read a couple of JSONB columns from Postgres DB in Java and I am trying to use the @JdbcTypeCode(SqlTypes.JSON) annotation to do so following this reference on stack overflow

The entity object is similar to this:

@Table (name = "test")
@Data
@ALLArgsConstructor @NoArgsConstructor
@Builder
public class TestEntity i
@Id
@GeneratedValue(strategy = GenerationType. IDENTITY)
@Column (name = "id", columnDefinition = "INT")
private Integer id;
@JdbcTypeCode (SqlTypes.JSON)
@Column (name = "custom", columnDefinition = "JSONB")
private CustomClass custom;
@JdbcTypeCode (SqlTypes.JSON)
@Column (name = "headers", columnDefinition = "JSONB")
private MultiValueMap<String, String> headers;

The conversion for the CustomClass works fine but for the MultiValueMap<String, String>, I am getting the following error: [org.springframework.dao.InvalidDataAccessApiUsageException: Could not deserialize string to java type: JsonJavaType(org.springframework.util.MultiValueMap<java.lang.String, java.lang.String>)]

How can I still use this annotation to convert the JSONB column to a MultiValueMap or even just a map?

Please note that the conversion works fine when I have a MultiValueMapConverter that implements the AttributeConverter from Jakarta and use @convert(converter = MultiValueMapConverter.class) annotation on the field. In the MultiValueMapConverter, we are essentially using jackson object mapper to convert the JSON into the MultiValueMap. In that case, the code would look like this:

    @Convert(converter = MultiValueMapConverter.class)
    @Column(name = "headers", columnDefinition = "JSONB")
    private MultiValueMap<String, String> headers;

I am trying to use the @JdbcTypeCode annotation or something similar so that we don't need to manually convert the JSON similar to the other column I have that's being automatically handled by the hibernate. Thanks in advance!

4
  • You either create feature request or fix it yourself. There is no ready to use solution. Hibernate is not aware of that spring class. Commented Sep 13, 2024 at 17:24
  • @talex Thanks for your reply. So, you are saying hibernate can't convert to MultiValueMap? Is there a way I can add that functionality just in my code and use that annotation? Commented Sep 13, 2024 at 19:04
  • I found this article to add a custom type: baeldung.com/hibernate-custom-types, but it almost doesn't seem worth it compared to the solution I already have Commented Sep 13, 2024 at 19:41
  • You already know how to use @Convert. I don't know what answer you looking for. Commented Sep 13, 2024 at 19:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.