JavaEE 8 - JPA Integer List Converter, Comma seperated string

Jpa Convert from JPA allows to write a custom db converter.
We can store a list of integers in a comma seperated way and use therefore an characterfield:

1
2
3
4
5
6
7
8
9
10
11
12
@Converter
public class IntegerListConverter implements AttributeConverter<List<Integer>, String> {
@Override
public String convertToDatabaseColumn(List<Integer> attribute) {
return attribute.stream().map(Object::toString).collect(Collectors.joining(","));
}

@Override
public List<Integer> convertToEntityAttribute(String dbData) {
return Arrays.asList(dbData.split("\\s*,\\s*")).stream().map(Integer::valueOf).collect(Collectors.toList());
}
}

How to use:

1
2
3
@Convert(converter = IntegerListConverter.class)
@Column(name = "what_ever")
private List<Integer> whatEver = Collections.emptyList();