简体   繁体   中英

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

@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

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)

You also should use the @Enumerated in the entity.

When you want to save the values as string you can use @Enumerated(EnumType.STRING)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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