簡體   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