【发布时间】:2019-10-28 18:02:02
【问题描述】:
我有一个 spring boot 项目(在 linux 上)和一个简单的数据库,它由两个实体组成 - 说法和标签。 Saying 可以有多个标签,每个标签可以用来标记多个说法,所以是多对多的关系。表saying_tags 用于使用它们的ID 连接说法和标签。
当我尝试使用此查询选择具有确切指定标签的所有说法,并且该查询选择了某些内容(结果不为空)时,休眠抛出异常说 Caused by: java.sql.SQLException: Column 'id' not found. 但列 id 甚至不存在于此询问。当我尝试从终端或 DBeaver 执行它时,它工作正常,并返回我所期望的。
我什至尝试使用确切的参数对查询进行硬编码,以确保 hibernate 不会更改查询中的任何内容,并且仍然失败。当休眠将查询记录到控制台时,我会从字面上复制查询,并且它在终端中运行良好。
这是带有硬编码值的查询和扩展JpaRepository<Saying, Long> 的接口内执行它的方法:
@Query(value="select saying_id from saying_tags group by saying_id having count(distinct case when tags_id in (33) then tags_id else 0 end) = 1 and min(case when tags_id in (33) then tags_id else 0 end) > 0", nativeQuery=true)
public List<Saying> getSayingsContainingExactlyGivenTags();
【问题讨论】:
-
你是如何使用 Hibernate 执行这个查询的。
-
@M.Deinum 我有一个扩展 JpaRepository
的接口,以及一个带有 @Query(value="query" , nativeQuery=true)的方法。 -
请将该代码添加到您的问题中。
标签: sql hibernate spring-boot jpa