Open query in hibernate
I am using struts and hibernate in my problem. I tried the following query
String hql ="insert into "+
"OPENQUERY(OracleLinkedServer, \'SELECT * FROM report_access_log\') "+
"(CALLINGHOST, ACCESSTIMESTAMP, HTTPREQUESTMETHOD, ACCESSURL,"+
"HTTPRESPONSECODE, HTTPRESPONSETIMEMILLI, USERNAME, REPORTNAME, ID)"+
" values "+
"(:CALLINGHOST,:ACCESSTIMESTAMP,:HTTPREQUESTMETHOD,:ACCESSURL,:HTTPRESPONSECODE," +
":HTTPRESPONSETIMEMILLI,:USERNAME,:REPORTNAME,"+
"(select * from OPENQUERY(OracleLinkedServer,"+
"\'select SQ_RPT_ACC_LOG_ID.nextval from dual\')))";
Query query=session.createQuery(hql);
query.setString("CALLINGHOST", userLogReport.get(0).toString());
query.setDate("ACCESSTIMESTAMP", (Date)userLogReport.get(1));
query.setString("HTTPREQUESTMETHOD", userLogReport.get(2).toString());
query.setString("ACCESSURL", userLogReport.get(3).toString());
query.setString("HTTPRESPONSECODE", userLogReport.get(4).toString());
query.setInteger("HTTPRESPONSETIMEMILLI", (Integer)userLogReport.get(5));
query.setString("USERNAME", userLogReport.get(6).toString());
query.setString("REPORTNAME", userLogReport.get(7).toString());
The query printed on the console is as follows
insert into OPENQUERY(OracleLinkedServer, 'SELECT * FROM开发者_StackOverflow report_access_log')
(CALLINGHOST, ACCESSTIMESTAMP, HTTPREQUESTMETHOD, ACCESSURL,HTTPRESPONSECODE,
HTTPRESPONSETIMEMILLI, USERNAME, REPORTNAME, ID) values
(:CALLINGHOST,:ACCESSTIMESTAMP,:HTTPREQUESTMETHOD,:ACCESSURL,:HTTPRESPONSECODE:HTTPRESPONSE
TIMEMILLI,:USERNAME,:REPORTNAME,(select * from OPENQUERY(OracleLinkedServer,'select
SQ_RPT_ACC_LOG_ID.nextval from dual')))
i get a query syntax exception at column no 79 which is (CALLINGHOST,...
But when i ran the query in SQL it is getting executed. The query is as follows
insert into OPENQUERY(OracleLinkedServer, 'SELECT * FROM report_access_log')
(CALLINGHOST, ACCESSTIMESTAMP, HTTPREQUESTMETHOD, ACCESSURL,HTTPRESPONSECODE,
HTTPRESPONSETIMEMILLI, USERNAME, REPORTNAME, ID) values
('10.87.192.246','GET','/cci/bby/ImageViewer/viewImages.action','200',6,'su','Insert
Review',(select * from OPENQUERY(OracleLinkedServer,'select SQ_RPT_ACC_LOG_ID.nextval
from dual')))
Please explain the problem and provide me a solution for executing it from Java. Thanks in advance.
HQL and SQL are two different languqges. HQL works on Hibernate entities, their properties, and associations between them. SQL works on database tables and columns.
Use
Query query = session.createSQLQuery(sql);
rather than
Query query = session.createQuery(hql);
精彩评论