4

Basically everything is in the title.

I have a column in my DB which is a varchar[].

I really would like to map it to a Java/Kotlin enum. We've already got this working to fetch it as a list of Strings (through com.vladmihalcea:hibernate-types and StringArrayType), but not with a mapping to an enum. Do you know if this is possible?

Since we know how to map a varchar to an enum, and a varchar[] to a collection of String, I would be tempted to think that this should possible, but I didn't succeed yet.

Here would be a simple sample of my current configuration:

CREATE TABLE test(my_values varchar[]) ;
INSERT INTO test(my_values) values ('{VAL1, VAL2}')
@Entity
@Table(name = "test")
data class DbTest(
        @Column(name = "my_values")
        val myValues: List<Values>
)

enum class Values {
       VAL1, VAL2
}

I tried this: https://vladmihalcea.com/map-postgresql-enum-array-jpa-entity-property-hibernate/ which looks pretty good but you have to define the enum in the DB and we don't want that.

Thanks!

6 Answers 6

5

If using Hibernate version > 6 this changed and should look like this:

@Type(StringArrayType.class)
@Column(
    name = "sensor_names",
    columnDefinition = "text[]"
)
private String[] sensorNames;

@Type(IntArrayType.class)
@Column(
    name = "sensor_values",
    columnDefinition = "integer[]"
)
private int[] sensorValues;

@Type(
    value = EnumArrayType.class,
    parameters = @Parameter(
        name = AbstractArrayType.SQL_ARRAY_TYPE,
        value = "sensor_state"
    )
)
@Column(
    name = "sensor_states",
    columnDefinition = "sensor_state[]"
)
private SensorState[] sensorStates;

Note: EnumArrayType comes from library HypersistenceUtils

Sign up to request clarification or add additional context in comments.

Comments

1

Previous answer does not work for me, but this working:

    TypeDef(
      name = "enums-array",
      typeClass = ListArrayType::class,
      parameters = [Parameter(name = EnumArrayType.SQL_ARRAY_TYPE, value = "varchar")]
    )

1 Comment

This is no longer possible with Hibernate >6 since TypeDef has been removed (see answer above)
0

I'm posting my solution, I didn't succeed to get a List<Values>, although I got an Array<Values> which was fine with me.

@Entity
@Table(name = "test")
@TypeDef(
        name = "values-array",
        typeClass = EnumArrayType::class,
        defaultForType = Array<Values>::class,
        parameters = [
          Parameter(
                  name = EnumArrayType.SQL_ARRAY_TYPE,
                  value = "varchar"
          )
        ]
)
data class DbTest(
        @Type(type = "values-array")
        @Column(name = "my_values", columnDefinition = "varchar[]")
        val myValues: Array<Values>
)

enum class Values {
       VAL1, VAL2
}

This is working like a charm and I can map my array to a list and vice versa quite easily, which is ok.

Hoping this will help someone someday ;)

1 Comment

This is no longer possible with Hibernate >6 since TypeDef has been removed (see answer above)
0

I'm just posting one more example for those who work with Kotlin and Hibernate > 6.

Recently I've migrated from Hibernate 5 to Hibernate 6 and had to ajust declarations for updated io.hypersistence:hypersistence-utils-hibernate-63:3.7.5

Namelly, I have Kotlin enum Causer which is mapped to simple postgres string array text[]:

...
    @Type(
        value = ListArrayType::class,
                parameters = [
            Parameter(
                name = EnumArrayType.SQL_ARRAY_TYPE,
                value = "text"
            )
        ]
    )
    @Column(
        name = "causer_list",
        columnDefinition = "text[]"
    )
    var causerList: List<Causer>? = null
...

I hope this will be usefull for someone else.

Comments

0

Hibernate 6+ plain solution:

...
@JdbcTypeCode(SqlTypes.ARRAY)
@Enumerated(EnumType.STRING)
val statusGroup: List<Status>? = null
...

enum class Status {
    COMPLETED, DECLINED, PENDING
}

Or omit Enumerated if you are ok with enum ordinal in DB

1 Comment

This does not work. It just creates "varchar array" column instead of "status_enum []".
0

For Hibernate version > 6 just add

@JdbcTypeCode(SqlTypes.ARRAY) List first;

@JdbcTypeCode(SqlTypes.ARRAY) List second;

1 Comment

When answering older questions, please make sure you provide either a novel solution, or a significantly better explanation than existing answers. If an existing answer already has your preferred solution, consider upvoting that answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.