英文:
Hibernate mapping between Postgres array of varchar and a Java/Kotlin collection of enum
问题
基本上所有的内容都在标题中。
我在我的数据库中有一个 varchar[]
类型的列。
我真的希望能将它映射到 Java/Kotlin 的 enum
类型。我们已经成功地将其作为 String
列表获取(通过 com.vladmihalcea:hibernate-types
和 StringArrayType
),但是没有映射到枚举类型。您知道是否可能实现这一点?
由于我们知道如何将 varchar
映射到 enum
,将 varchar[]
映射到 String
集合,我会倾向于认为这应该是可能的,但我尚未成功。
这里是我当前配置的简单示例:
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
}
我尝试了这个:https://vladmihalcea.com/map-postgresql-enum-array-jpa-entity-property-hibernate/,看起来非常不错,但是你必须在数据库中定义枚举,而我们不想要那样做。
谢谢!
英文:
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 String
s (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!
答案1
得分: 2
之前的答案对我不起作用,但以下方法有效:
TypeDef(
name = "enums-array",
typeClass = ListArrayType::class,
parameters = [Parameter(name = EnumArrayType.SQL_ARRAY_TYPE, value = "varchar")]
)
英文:
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")]
)
答案2
得分: 1
我在这里发布我的解决方案,尽管我没有成功获得 List<Values>
,但我获得了一个 Array<Values>
,这对我来说还不错。
@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
}
这在工作中表现得非常好,我可以轻松地将我的数组映射到列表,反之亦然,这是可以接受的。
希望这能在将来帮助到某人;)
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论