简体   繁体   English

在枚举列类型上休眠 @Filter

[英]Hibernate @Filter on enum column type

This is a similar question to this: Hibernate @Filter collection of enums这是一个与此类似的问题: Hibernate @Filter collection of enums

But in that question it looks like the asker has an enum type of varchar on the database, which is working fine for me.但是在那个问题中,看起来提问者在数据库上有一个枚举类型的 varchar,这对我来说很好用。

My isue is when trying to use hibernate filter annotations on entity values that have an enum column type in the database.我的问题是在尝试对数据库中具有枚举列类型的实体值使用休眠过滤器注释时。 Let's say the enum type of the column is called "database_enum"假设列的枚举类型称为“database_enum”

Take entity:取实体:

@Entity
@Table(name = "table_1")
@TypeDefs(
  TypeDef(name = "enum", typeClass = PostgreSQLEnumType::class)
)
@Mockable
class table1{

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  var id: Int = 0

  ...

  @Enumerated(EnumType.STRING)
  @Type(type = "enum")
  lateinit var enumColumn: EnumClass
}

With EnumClass:使用枚举类:

enum class EnumClass{
  TYPEA(EnumSubClass.ONE),
  TYPEB(EnumSubClass.TWO),
  ...
  TYPEN(EnumSubClass.N);

  val category: EnumSubClass

  constructor(category: EnumSubClass) {
    this.category = category
  }

  companion object {
    ...
  }
}

The filter is on a parent entity:过滤器位于父实体上:

@Entity
@Table(name = "mla_simulation_turbine")
@FilterDefs(
  FilterDef(name = "enumTypeFilter", parameters = [ParamDef(name="enumTypeParam", type="string")])
)
@Mockable
class ParentEntity{

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  var id: Int = 0


  @OneToMany(mappedBy = "...", fetch = FetchType.LAZY)
  @Filters(
    Filter(name = "enumTypeFilter", condition="enumColumn= :enumTypeParam")
  )
  var components = mutableSetOf<EnumClass>()


}

And the filter being set like this:过滤器设置如下:

val existingSession = em.unwrap(Session::class.java)
      existingSession.enableFilter("enumTypeFilter")
        .setParameter("enumTypeParam", EnumClass.TYPEA.toString())

The query in hibernate logging shows the filter as:休眠日志中的查询将过滤器显示为:

SELECT ...
where
        enum_column.enum_type= ? 

Finally the exception raised when I load the enum entity is an SQLGrammarException caused by:最后,当我加载枚举实体时引发的异常是 SQLGrammarException 引起的:

Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: database_enum= character varying
  Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
  Position: 925

I'm wondering if I need to refactor things and just keep the enum on the kotlin side and give it a varchar type in the database.我想知道我是否需要重构事物并只将枚举保留在 kotlin 端,并在数据库中给它一个 varchar 类型。 I have not been able to find a similar post/issue with any helpful answers but here is what I have looked at:我无法找到类似的帖子/问题以及任何有用的答案,但这是我所看到的:

https://forum.hibernate.org/viewtopic.php?f=1&t=1044249&view=previous https://forum.hibernate.org/viewtopic.php?f=1&t=1044249&view=previous

hibernate criteria filtering on a set of enum values 对一组枚举值进行休眠条件过滤

and a few others.和其他一些人。

Any help or tips here would be appreciated.任何帮助或提示在这里将不胜感激。 Thanks!谢谢!

Ok, got the solution for anyone that could benefit from it.好的,为任何可以从中受益的人找到了解决方案。

You need to cast the column to text rather than cast the parameters to enum....您需要将列转换为文本而不是将参数转换为枚举....

cast(enumColumn as text) in (:enumTypeParam)

Works like a charm.奇迹般有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM