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!
@Convert. I don't know what answer you looking for.