谁知道hibernate在执行操作触发器sql语句代码时报语法错误,但是sql语句在mySQL中能正确执行,错误的原因在哪?(执行的方法为createSQLQuery(sql);)
报错信息:org.hibernate.exception.SQLGrammarException: could not execute native bulk manipulation query
,哪位高手帮帮忙啊! 在线求助。

解决方案 »

  1.   

    真没见人用hibernate来执行触发器的
      

  2.   

    /**
     * // 读取脚本文件并进行追加操作
     * @param reportName 数据库表名称
     * @param reports 表中属性内容
     * @param filePath   脚本文件所放置位置
     * @param reportFields  表字段字符串
     */
    public void readAndOperate(String reportName,
    List<String> reports, String filePath, String reportFields) {
    // TODO Auto-generated method stub
    //StringBuffer buffer = new StringBuffer();

    RandomAccessFile randomFile = null;
    try {
    // 打开一个随机访问文件流,按读写方式
    randomFile = new RandomAccessFile(filePath, "rw");

    StringBuffer stA = new StringBuffer();

    //---------------------获取文件开头部分------------------------
    byte[] b = new byte[300];
    randomFile.read(b, 0, 237);
    String s = new String(b,0,237);
    stA.append(s.trim());

    //------------------------声明游标------------------------------
    stA.append(" DECLARE " + reportName + "Cursor CURSOR FOR SELECT ID, TOTAL FROM " + reportName + " WHERE " +
    "TIME=date_format(NEW.TIME, '%Y-%m-%d %H-%i-00') AND ");

    for(int j = 0 ; j < reports.size() - 1;j++){
    stA.append(reports.get(j) + " = NEW." + reports.get(j) + " AND ");
    }
    stA.append(reports.get(reports.size() - 1) + " = NEW." + reports.get(reports.size() - 1) + "; ");
    //System.out.println("stA---> " + stA.toString());

    // ----------------------拼接中间原有部分--------------------------

    // 237 指的是头部信息直到 BOOLEAN; 4指的是文件末尾END;四个字符,要去掉这四个字符,此为固定
    randomFile.seek(237);
    int len = (int)randomFile.length() - 241;
    byte[] c = new byte[len];
    randomFile.read(c,0,len);
    stA.append(new String(c,0,len).trim());

    //----------------------拼接到尾部(新)部分--------------------------

    stA.append(" OPEN " + reportName + "Cursor; SET recordId=0;  SET recordTotal=0;  SET fetchSeqOk=FALSE; FETCH " + reportName + "Cursor " +
    "INTO recordId, recordTotal;IF fetchSeqOk THEN INSERT INTO " + reportName + "(TIME," + reportFields + " TOTAL) VALUE (" +
    "date_format(NEW.TIME, '%Y-%m-%d %H-%i-00'),");

    for(int z = 0 ; z < reports.size();z++){
    stA.append("NEW." + reports.get(z) + ", ");
    }
    stA.append("1);ELSE UPDATE " + reportName + " SET TOTAL = recordTotal+1 WHERE ID=recordId;" +
    "END IF;" +
    "CLOSE " + reportName + "Cursor; END");
    //------------------------拼接结束-------------------------------------System.out.println("拼接的语句:---> " + stA.toString());

    // 创建和删除文件
    createAndDelFile(filePath,stA);

    } catch (IOException e){
    e.printStackTrace();
    }finally{
    try {
    randomFile.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }错误:org.hibernate.exception.SQLGrammarException: could not execute native bulk manipulation query