1
votes

My application uses Spring Boot,JPA and Crnk Framework and mariadb as the database.

I have a field defined like this

@Column(name = "baskets", columnDefinition = "TEXT")
@Convert(converter = UUIDListConverter.class)
private List<UUID> baskets;

And in mariadb it looks like this

enter image description here enter image description here

And my mapper is written like this

@Component
public class UUIDListConverter implements AttributeConverter<List<UUID>, String> {

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public List<UUID> convertToEntityAttribute(String dbData) {
        try {
            return objectMapper.readValue(dbData, new TypeReference<List<UUID>>() {});
        } catch (JsonProcessingException e) {
            throw new IllegalArgumentException(
                    "Unable to convert attribute JSON from database format: " + e.getMessage(), e);
        }
    }
}

I tried updating the value of the baskets with the following query in mariadb. I ran this query in mysql gui tool for mariadb.

update table_name set baskets = (UUID()) where code = 'XXX';

It inserted the value into the baskets column. The value that got inserted into the column is this

a5cff67a-6098-11ec-9745-acde48001122

But when i try to read the columns and convert to model object. it gives me the following error

org.springframework.orm.jpa.JpaSystemException: Error attempting to apply AttributeConverter; nested exception is javax.persistence.PersistenceException: Error attempting to apply AttributeConverter

Caused by: java.lang.IllegalArgumentException: Unable to convert attribute JSON from database format: Unrecognized token 'a5cff67a': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
 at [Source: (String)"a5cff67a-6098-11ec-9745-acde48001122"; line: 1, column: 9]
    at util.UUIDListConverter.convertToEntityAttribute(UUIDListConverter.java:34)
    at util.UUIDListConverter.convertToEntityAttribute(UUIDListConverter.java:12)
    at org.hibernate.metamodel.model.convert.internal.JpaAttributeConverterImpl.toDomainValue(JpaAttributeConverterImpl.java:45)
    at org.hibernate.type.descriptor.converter.AttributeConverterSqlTypeDescriptorAdapter$2.doConversion(AttributeConverterSqlTypeDescriptorAdapter.java:140)
    ... 148 more
Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'a5cff67a': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
 at [Source: (String)"a5cff67a-6098-11ec-9745-acde48001122"; line: 1, column: 9]
  1. Is it because the data type of baskets column is "Text". ?

2.Should the data type be "BINARY (16) to hold the UUID values. ?

Any other suggestions/ help

1

1 Answers

0
votes

1.

Is it because the data type of baskets column is "Text". ?

I doubt so!

I think:

objectMapper.readValue(dbData, new TypeReference<List<UUID>>() {})

..throws exception, because:

  1. the data (a5cff67a-6098-11ec-9745-acde48001122, no quotes, no braces, just plain uuid) doesn't look like valid json!

  2. even if (update table_name set baskets = ('["'||UUID()||'"]') where code = 'XXX';, trying to produce valid json), which converter should objectMapper call!?!?! ;) (-> stackoverflow!)


2.

Should the data type be "BINARY (16) to hold the UUID values. ?

Naah!

Best, probably: JSON


Any other suggestions/ help

  • put (only) valid json into the database.
  • (test) Omit/delete/forget (at least reconsider) your converter, try how jackson handles this.