列表的Hibernate参数绑定问题



在我的一个存储库中有以下方法

@Query(value = "select resource_id from tag where resource_type = :resourceType and value in :tags", nativeQuery = true)
List<String> search(String resourceType, List<String> tags);

如果我将其作为一个原始sql查询运行,那么正如预期的那样,我将获得几个元素:

select resource_id from tag where resource_type = 'color' and value in ('red', 'blue');

然而,如果我从我的存储库中运行搜索方法或从类EntityManager的方法createNativeQuery中运行等效方法,则如果";标签";包含多个值。

2022-04-06 04:36:49.726 DEBUG 14368 --- [nio-8080-exec-1] org.hibernate.SQL                        : select resource_id from tag where resource_type = ? and value in (?)
Hibernate: select resource_id from tag where resource_type = ? and value in (?)
2022-04-06 04:36:49.731 TRACE 14368 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [color]
2022-04-06 04:36:49.732 TRACE 14368 --- [nio-8080-exec-1] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - [red]

到目前为止一切都很好,我收到了几个结果,但如果我在";标签";。。。

2022-04-06 04:36:55.041 DEBUG 14368 --- [nio-8080-exec-6] org.hibernate.SQL                        : select resource_id from tag where resource_type = ? and value in (?)
Hibernate: select resource_id from tag where resource_type = ? and value in (?)
2022-04-06 04:36:55.041 TRACE 14368 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [VARCHAR] - [color]
2022-04-06 04:36:55.041 TRACE 14368 --- [nio-8080-exec-6] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - [res,blue]

这一次,我收到了0个元素作为结果(这与原始sql结果不同(。令我惊讶的是这条日志行:

Hibernate: select resource_id from tag where resource_type = ? and value in (?)

我预期:

select resource_id from tag where resource_type = ? and value in (?, ?)

我做错了什么?

我使用的是spring-boot 2.6.6和与之捆绑的默认hibernate版本(5.6.7.Final,如果我相信Intellij的话(

实体类中的列的名称可能与数据库中的列名称不同,例如对于resource_id,实体类中变量的名称可能是resourceId。

@Query(value = "select t.resourceId from tag t where t.resourceType = :resourceType and t.value in :tags", nativeQuery = true)
List<String> search(@Param("resourceType") String resourceType, @Param("tags") List<String> tags);

最新更新