Но я не смог найти рабочего решения для своего случая. Итак, вот мой код:
Код: Выделить всё
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.inject.Singleton;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;
import lombok.RequiredArgsConstructor;
import java.io.IOException;
import java.util.List;
@Singleton
@RequiredArgsConstructor
@Converter(autoApply = true)
public class JsonbConverter implements AttributeConverter {
private final ObjectMapper objectMapper;
@Override
public String convertToDatabaseColumn(List attribute) {
try {
return attribute == null ? null : objectMapper.writeValueAsString(attribute);
} catch (JsonProcessingException e) {
throw new RuntimeException("Error converting to JSON", e);
}
}
@Override
public List convertToEntityAttribute(String dbData) {
try {
return dbData == null ? null : objectMapper.readValue(dbData,
objectMapper.getTypeFactory().constructCollectionType(List.class, Metadata.class));
} catch (IOException e) {
throw new RuntimeException("Error parsing JSON", e);
}
}
}
Код: Выделить всё
@Data
@Introspected
@JsonInclude(JsonInclude.Include.NON_NULL)
@Schema(title = METADATA_TITLE, description = METADATA_DESCRIPTION)
public class Metadata {
@NotBlank
@Size(min = 1, max = 1000)
private String field;
@NotBlank
@Size(min = 1, max = 1000)
private String value;
@JsonCreator
public Metadata(@JsonProperty("field") String field,
@JsonProperty("value") String value) {
this.field = field;
this.value = value;
}
}
Код: Выделить всё
@Value
@Embeddable
class StoredIndexingDetails {
@Nullable
// @Convert(converter = JsonbConverter.class)
@TypeDef(type = DataType.JSON, converter = JsonbConverter.class)
// @Column(name = "indexing_primary_metadata", columnDefinition = "jsonb")
List primaryMetadata;
}
Код: Выделить всё
@Column(name = "indexing_primary_metadata", columnDefinition = "jsonb")Моя ошибка следующая:
Код: Выделить всё
Error executing PERSIST: ERROR: column \"indexing_primary_metadata\" is of type jsonb but expression is of type character varying\n Hint: You will need to rewrite or cast the expression.
Код не выдал ошибку, но в поле БД было нулевое значение.
Есть предложения?
Подробнее здесь: https://stackoverflow.com/questions/792 ... sonb-field
Мобильная версия