Reputation: 2325
Do you know if Micronaut Data JDBC supports Postgres array using Java array/collections or do I need to define a custom type? If so how? I looked at https://micronaut-projects.github.io/micronaut-data/latest/guide/#jdbcDataTypes but did not see any information on array types.
Upvotes: 2
Views: 1380
Reputation: 61
I had similar problem to map Array of Enum type to Postgresql Array type column. I did use custom attribute converter for 1) convert enum-array to string-array and 2) PgArray object to enum-array.
@Singleton
class CustomEnumTypesConverter : AttributeConverter<Array<CustomEnum>, Any> {
override fun convertToPersistedValue(
entityValue: Array<CustomEnum>,
context: ConversionContext
) = entityValue.map { it.name }.toTypedArray()
override fun convertToEntityValue(
persistedValue: Any,
context: ConversionContext
) = ((persistedValue as PgArray).array as Array<String>)
.map { CustomEnum.valueOf(it) }.toTypedArray()
}
enum class CustomEnum {
VALUE_1, VALUE_2, VALUE_3
}
@Entity
data class MyEntity(
....
@field:TypeDef(type = DataType.OBJECT, converter = CustomEnumTypesConverter::class)
val customEnumTypes: Array<CustomEnum>,
...
)
The interface to implement in converter is
import io.micronaut.data.model.runtime.convert.AttributeConverter
I've micronautVersion=3.5.3
Upvotes: 3
Reputation: 2325
Looks like array types does not work out of the box unless you specify the column data type as DataType.OBJECT
e.g.
@MappedEntity
public class Article{
//...
@MappedProperty(type = DataType.OBJECT)
private String[] labels;
}
Upvotes: 1