繁体   English   中英

spring jpa query where子句与枚举类型匹配

[英]spring jpa query where clause with enum type matching

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

@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类型所以我得到了execption

错误

[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.

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

@Query(value =“从schema_task_test.test_table中选择distinct task_id,其中type = cast(:type as type)”,nativeQuery = true)

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

如果要将值保存为字符串,可以使用@Enumerated(EnumType.STRING)

暂无
暂无

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

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