简体   繁体   中英

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

I wrote a SQL server query with three table left outer joins and it executed well in SQL SERVER console.

Here is the query

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

But when I use this query with my application which uses hibernate, hibernate change the query as below.

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 

In this changed Hibernate query gives me following error when running the application.

  • The multi-part identifier "p.description" could not be bound.
  • org.hibernate.exception.SQLGrammarException: The multi-part identifier "p.description" could not be bound.

  • Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The multi-part identifier "p.description" could not be bound.

I found same type of questions in stack overflow , but didn't find any correct answer for issue.

Is any one can describe why it happen and how to fix this issue it will be great helpful.Thanks in advance.

I found the answer.

In my hibernate configuration file previously i added hibernate dialect property as below.

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

But it was wrong.

I change the hibernate dialect property as below.

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

This works fine.

Thank you.

can you try and do this with a native query instead? as shown below?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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