Skip to content

Custom converters not working for collecton-like properties #2078

@denniseffing

Description

@denniseffing

Hi,
it seems that it is impossible to save an entity that includes a collection-like property using a custom type because the converting process doesn't work properly.

The following entity can not be saved, even with the supplied custom converters:

class SomeEntity {
    // ... id, persistence creator etc.
   
    List<CustomType> types;
}

record CustomType(String value) { }

class CustomTypeToStringConverter implements Converter<CustomType, String> {

	@Override
	public String convert(CustomType source) {
		return source.value;
	}
}

class StringToCustomTypeConverter implements Converter<String, CustomType> {

	@Override
	public Region convert(String source) {
		return new CustomType(source);
	}
}

The only way to make this work is to create another wrapper type like this:

record CustomTypes(List<CustomType> types) {}

// converters for CustomTypes record

I've done some digging:
The derived column type of every collection-like property that is not an entity is the Array class: spring-projects/spring-data-relational@3117465d:spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/MappingJdbcConverter.java#180

When a collection is written, every value of it is mapped and stored in an ArrayList (as expected), however, the code then checks if the mapped ArrayList is an instance of the derived column type (i.e. Array) before returning it, which can never be true.
spring-projects/spring-data-relational@3117465d:spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/MappingRelationalConverter.java#770

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions