Hibernate在Postgres的varchar数组和Java/Kotlin的枚举集合之间的映射

huangapple go评论105阅读模式
英文:

Hibernate mapping between Postgres array of varchar and a Java/Kotlin collection of enum

问题

基本上所有的内容都在标题中。

我在我的数据库中有一个 varchar[] 类型的列。

我真的希望能将它映射到 Java/Kotlin 的 enum 类型。我们已经成功地将其作为 String 列表获取(通过 com.vladmihalcea:hibernate-typesStringArrayType),但是没有映射到枚举类型。您知道是否可能实现这一点?

由于我们知道如何将 varchar 映射到 enum,将 varchar[] 映射到 String 集合,我会倾向于认为这应该是可能的,但我尚未成功。

这里是我当前配置的简单示例:

  1. CREATE TABLE test(my_values varchar[]) ;
  2. INSERT INTO test(my_values) VALUES ('{VAL1, VAL2}')
  1. @Entity
  2. @Table(name = "test")
  3. data class DbTest(
  4. @Column(name = "my_values")
  5. val myValues: List<Values>
  6. )
  7. enum class Values {
  8. VAL1, VAL2
  9. }

我尝试了这个: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 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:

  1. CREATE TABLE test(my_values varchar[]) ;
  2. INSERT INTO test(my_values) values (&#39;{VAL1, VAL2}&#39;)
  1. @Entity
  2. @Table(name = &quot;test&quot;)
  3. data class DbTest(
  4. @Column(name = &quot;my_values&quot;)
  5. val myValues: List&lt;Values&gt;
  6. )
  7. enum class Values {
  8. VAL1, VAL2
  9. }

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

之前的答案对我不起作用,但以下方法有效:

  1. TypeDef(
  2. name = "enums-array",
  3. typeClass = ListArrayType::class,
  4. parameters = [Parameter(name = EnumArrayType.SQL_ARRAY_TYPE, value = "varchar")]
  5. )
英文:

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

  1. TypeDef(
  2. name = &quot;enums-array&quot;,
  3. typeClass = ListArrayType::class,
  4. parameters = [Parameter(name = EnumArrayType.SQL_ARRAY_TYPE, value = &quot;varchar&quot;)]
  5. )

答案2

得分: 1

我在这里发布我的解决方案,尽管我没有成功获得 List<Values>,但我获得了一个 Array<Values>,这对我来说还不错。

  1. @Entity
  2. @Table(name = "test")
  3. @TypeDef(
  4. name = "values-array",
  5. typeClass = EnumArrayType::class,
  6. defaultForType = Array<Values>::class,
  7. parameters = [
  8. Parameter(
  9. name = EnumArrayType.SQL_ARRAY_TYPE,
  10. value = "varchar"
  11. )
  12. ]
  13. )
  14. data class DbTest(
  15. @Type(type = "values-array")
  16. @Column(name = "my_values", columnDefinition = "varchar[]")
  17. val myValues: Array<Values>
  18. )
  19. enum class Values {
  20. VAL1, VAL2
  21. }

这在工作中表现得非常好,我可以轻松地将我的数组映射到列表,反之亦然,这是可以接受的。

希望这能在将来帮助到某人;)

英文:

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

  1. @Entity
  2. @Table(name = &quot;test&quot;)
  3. @TypeDef(
  4. name = &quot;values-array&quot;,
  5. typeClass = EnumArrayType::class,
  6. defaultForType = Array&lt;Values&gt;::class,
  7. parameters = [
  8. Parameter(
  9. name = EnumArrayType.SQL_ARRAY_TYPE,
  10. value = &quot;varchar&quot;
  11. )
  12. ]
  13. )
  14. data class DbTest(
  15. @Type(type = &quot;values-array&quot;)
  16. @Column(name = &quot;my_values&quot;, columnDefinition = &quot;varchar[]&quot;)
  17. val myValues: Array&lt;Values&gt;
  18. )
  19. enum class Values {
  20. VAL1, VAL2
  21. }

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 Hibernate在Postgres的varchar数组和Java/Kotlin的枚举集合之间的映射

huangapple
  • 本文由 发表于 2020年9月4日 23:20:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63743920.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定