Home > Back-end >  how to write innerjoin query using hibernate?
how to write innerjoin query using hibernate?

Time:04-08

I was trying to query using hibernate and im getting some alias and some other errors. can anyone help me to write code for below query

select columnA,x.columnB ,* from Table_A 
inner join Table_B  on x.tableA_ID = y.TableB_id
where x.columnC in (3,6) and x.columnD <> 1 and
y.Column2 >= '2021-02-05 15:47:01.000'

I have tried the below code,

String date="2021-02-05 15:47:01.000"
Query query = session.createSQLQuery("select x.columnA,x.columnB ,* from Table_A x\n"  
                "inner join Table_B y on x.tableA_ID = y.TableB_id\n"  
                "where x.columnC in (1,2) and x.columnD <> 1 and\n"  
                "y.Column2 >= '" date "'");
resultList= query.list();

error:

Encountered a duplicated sql alias [columnB] during auto-discovery of a native-sql query; nested exception is o

CodePudding user response:

Could you clarify if you are using native SQL or HQL? It seems to me that you specified table names instead of entity classes in the query.

CodePudding user response:

The issue is resolved by removing the ",*".

Query query = session.createSQLQuery("select x.columnA,x.columnB from Table_A x\n"  
                "inner join Table_B y on x.tableA_ID = y.TableB_id\n"  
                "where x.columnC in (1,2) and x.columnD <> 1 and\n"  
                "y.Column2 >= '" date "'");
  • Related