在hibernate中打开查询

时间:2011-09-13 09:34:01

标签: java hibernate openquery

我在我的问题中使用struts和hibernate。我尝试了以下查询

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());

控制台上打印的查询如下

insert into  OPENQUERY(OracleLinkedServer, 'SELECT * FROM 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')))

我在第79列获得了查询语法异常,即(CALLINGHOST,...

但是当我在SQL中运行查询时,它正在执行。查询如下

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')))

请解释问题并为我提供从Java执行它的解决方案。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

HQL和SQL是两种不同的语言。 HQL适用于Hibernate实体,它们的属性以及它们之间的关联。 SQL适用于数据库表和列。

使用

Query query = session.createSQLQuery(sql); 

而不是

Query query = session.createQuery(hql);