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

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

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 集合,我会倾向于认为这应该是可能的,但我尚未成功。

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

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 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 (&#39;{VAL1, VAL2}&#39;)
@Entity
@Table(name = &quot;test&quot;)
data class DbTest(
        @Column(name = &quot;my_values&quot;)
        val myValues: List&lt;Values&gt;
)

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 = &quot;enums-array&quot;,
      typeClass = ListArrayType::class,
      parameters = [Parameter(name = EnumArrayType.SQL_ARRAY_TYPE, value = &quot;varchar&quot;)]
    )

答案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&lt;Values&gt;, although I got an Array&lt;Values&gt; which was fine with me.

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

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 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:

确定