繁体   English   中英

MS SQL Server数据库中无法绑定多部分标识符

[英]The multi-part identifier could not be bound in ms sql server database

我编写了一个带有三个表左外部联接的SQL Server查询,它在SQL SERVER控制台中执行良好。

这是查询

select  [a].[audittraceid],  
 [a].[description], 
 [a].[remarks],  
 [a].[ip],  
 [a].[oldvalue],  
 [a].[newvalue],  
 [a].[lastupdateduser],  
 convert(varchar(19),[a].[lastupdatedtime],120) as lastupdatedtime,  
 convert(varchar(19),[a].[createdtime],120) as createdtime, 
 [a].[affectedkey],  
 [ur].[userrolecode] as userrolecode,  
 [ur].[description] as userroledes, 
 [p].[pagecode] as pagecode, 
 [p].[description] as pagedes,  
 [t].[description] as taskdes  
 From [TESTDB].[dbo].[Audittrace] [a] 
 left outer join [TESTDB].[dbo].[Userrole] [ur] on [a].[userrolecode]=[ur].[userrolecode]  
 left outer join [TESTDB].[dbo].[Page] [p] on [a].[pagecode]=[p].[pagecode]  
 left outer join [TESTDB].[dbo].[Task] [t] on [a].[taskcode]=[t].[taskcode]
 order by [a].[lastupdatedtime] desc

但是当我在使用休眠的应用程序中使用此查询时,休眠更改查询如下。

WITH query AS (
    SELECT inner_query.*, ROW_NUMBER() OVER (ORDER BY CURRENT_TIMESTAMP) as __hibernate_row_nr__ FROM ( 
        select TOP(10)  [a].[audittraceid] as page0_, 
        [a].[description] as page1_,  
        [a].[remarks] as page2_,  
        [a].[ip] as page3_,  
        [a].[oldvalue] as page4_,  
        [a].[newvalue] as page5_,  
        [a].[lastupdateduser] as page6_,  
        convert(varchar(19),[a].[lastupdatedtime],120) as lastupdatedtime,  
        convert(varchar(19),[a].[createdtime],120) as createdtime,  
        [a].[affectedkey] as page7_,  [ur].[userrolecode] as userrolecode,  
        [ur].[description] as userroledes,  [p].[pagecode] as pagecode,  
        [p].[description] as pagedes,  
        [t].[description] as taskdes  From [BIMPUTHDEV01].[dbo].[Audittrace] [a] 
        left outer join [TESTDB].[dbo].[Userrole] [ur] on [a].[userrolecode]=[ur].[userrolecode]  
        left outer join [TESTDB].[dbo].[Page] [p] on [a].[pagecode]=[p].[pagecode]  
        left outer join [TESTDB].[dbo].[Task] [t] on [a].[taskcode]=[t].[taskcode]   
        order by [a].[lastupdatedtime] desc 
    ) inner_query 
) 
SELECT 
page0_, 
page1_, 
page2_, 
page3_, 
page4_, 
page5_, 
page6_, 
lastupdatedtime, 
createdtime, 
page7_, 
userrolecode, 
userroledes, 
pagecode,  
[p].[description] as pagedes,  /*This line gives the error*/
[t].[description] as taskdes   
FROM query 

在此更改的Hibernate查询中 ,运行应用程序时出现以下错误。

  • 无法绑定多部分标识符“ p.description”。
  • org.hibernate.exception.SQLGrammarException:无法绑定多部分标识符“ p.description”。

  • 引起原因:com.microsoft.sqlserver.jdbc.SQLServerException:无法绑定多部分标识符“ p.description”。

我在堆栈溢出中发现了相同类型的问题,但没有找到任何正确的问题答案。

有谁能描述它发生的原因以及如何解决这个问题,这将对您大有帮助。

我找到了答案。

以前的休眠配置文件中,我添加了休眠方言属性,如下所示。

<property name="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</property>

但这是错误的。

我将休眠方言属性更改如下。

<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>

这很好。

谢谢。

您可以尝试使用本地查询来代替吗? 如下所示?

String sql = "SELECT * FROM EMPLOYEE";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Employee.class);
List results = query.list();

https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/querysql.html https://www.tutorialspoint.com/hibernate/hibernate_native_sql.htm

暂无
暂无

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

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