【问题标题】:spring jpa query where clause with enum type matching具有枚举类型匹配的spring jpa查询where子句
【发布时间】:2019-08-10 12:47:03
【问题描述】:

我使用spring-data-jpa与spring boot和postgreSql进行对象关系映射,并编写了自定义方法来获取task_id列与where条件匹配时的不同值

@Query(value = "select distinct task_id from schema_task_test.test_table where type =:type", nativeQuery = true)
public List<Integer> findDistinctTasks(@Param("type") String type);

但在数据库中 typeEnum 类型,所以我得到了执行

错误

[ERROR] 2019-03-19 16:33:45,006 http-nio-8080-exec-1 org.hibernate.engine.jdbc.spi.SqlExceptionHelper - {} - ERROR: operator does not exist: schema_task_test.type_enum = character varying

Hint: No operator matches the given name and argument types. You might need to add explicit type casts.

【问题讨论】:

  • 可以添加实体类吗?
  • 没有实体@mohammad_1m2这可能吗
  • 原生查询是另一个可以在没有实体的情况下使用的选项。

标签: java postgresql spring-boot spring-data-jpa


【解决方案1】:

在本机查询中,您必须将显式转换为数据库枚举类型。

@Query(value = "select distinct task_id from schema_task_test.test_table where type = cast(:type as type)", nativeQuery = true)

您还应该在实体中使用@Enumerated。

当你想将值保存为字符串时,你可以使用@Enumerated(EnumType.STRING)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 2013-12-18
    • 2017-01-22
    • 2020-04-15
    • 2013-07-19
    • 2017-05-25
    • 1970-01-01
    相关资源
    最近更新 更多