[英]spring jpa query where clause with enum type matching
I'm using the spring-data-jpa
for object relational mapping with spring boot and postgreSql, and wrote custom method to get the distinct value of task_id
column when matching to where condition 我正在使用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);
But in database type
is of Enum
type so I'm getting execption 但是在数据库type
是Enum
类型所以我得到了execption
Error 错误
[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.
In a native query you have to cast explicit to the database enum type. 在本机查询中,您必须将显式转换为数据库枚举类型。
@Query(value = "select distinct task_id from schema_task_test.test_table where type = cast(:type as type)", nativeQuery = true) @Query(value =“从schema_task_test.test_table中选择distinct task_id,其中type = cast(:type as type)”,nativeQuery = true)
You also should use the @Enumerated in the entity. 您还应该在实体中使用@Enumerated。
When you want to save the values as string you can use @Enumerated(EnumType.STRING) 如果要将值保存为字符串,可以使用@Enumerated(EnumType.STRING)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.