简体   繁体   English

如何在Hibernate本机查询中使用常量?

[英]How can I use constants in Hibernate native query?

I am trying to do a simple query against SQLServer in a Spring Data JPA repository using the @Query annotation to create a native query. 我正在尝试使用@Query批注对Spring Data JPA存储库中的SQLServer进行简单查询,以创建本机查询。 Hibernate is getting hold of it and cannot seem to parse out the constants (I think). Hibernate正在掌握它,而且似乎无法解析常量(我认为)。

The error is: org.hibernate.loader.custom.NonUniqueDiscoveredSqlAliasException: Encountered a duplicated sql alias [] during auto-discovery of a native-sql query 错误是: org.hibernate.loader.custom.NonUniqueDiscoveredSqlAliasException: Encountered a duplicated sql alias [] during auto-discovery of a native-sql query

The query is: 查询是:

select convert(varchar,((a.achan - a.freq) / 100))
       +'_'+
       convert(varchar,((a.bchan - a.freq) / 100))
     , convert(varchar,((a.bchan - a.freq) / 100)) 
from channel_src as a 
where a.discriminator = ?

Since every actual field is aliased to the actual table, and the complaint is about a duplicate NULL alias, I'm assuming that it doesn't like the 100's? 由于每个实际字段都别名为实际表,并且投诉是关于重复的NULL别名,因此我假设它不喜欢100的别名吗? If not, is there any way to divine what it's choking on? 如果没有,有什么办法可以判断出它正在窒息吗? There is no log line between resolution of the parameter and the rollback statement. 参数的解析和回滚语句之间没有日志行。

Edit: Here is the query in the context of the repository 编辑:这是存储库上下文中的查询

public interface ChannelMatrixRepository extends JpaRepository<ChannelMatrix,Integer>
{
    @Query(value = "select convert(varchar,((a.achan - a.freq) / 100)) +'_'+ convert(varchar,((a.bchan - a.freq) / 100)) , convert(varchar,((a.bchan - a.freq) / 100)) from channel_src as a where a.discriminator = ?1", nativeQuery = true)
    Map<String, String> findAllBySquelchLevel(int sk);
}

I believe this error: Encountered a duplicated sql alias [] 我相信这个错误:遇到重复的sql别名[]

Is caused by the fact that you have multiple columns in your select statement that are non-table-column-names. 由以下事实引起的:您的select语句中有多个非表列名称的列。 Giving each of these columns an alias should work. 给这些列中的每一个别名都应该起作用。 In other words, something like this should work: 换句话说,这样的事情应该起作用:

select 
  convert(varchar,((a.achan - a.freq) / 100)) +'_'+
       convert(varchar,((a.bchan - a.freq) / 100)) COLUMN_ONE
     , convert(varchar,((a.bchan - a.freq) / 100)) COLUMN_TWO
from channel_src as a 
where a.discriminator = ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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