你这执行的是纯sql,貌似有的hibernate版本不能直接用sql操作mysql数据库,我猜你没在别的方法里用SQLQuery,如果其他地方可以执行那我给你的办法你就不用看了。我当时遇到过hibernate不能用SQLQuery操作mysql。
解决办法:你的hibernate配置文件里一定有个地方配置方言的如:
org.hibernate.dialect.MySQLDialect
你需要将MySQLDialect.java重写,我当时感觉改变它的jar里的文件比较麻烦,就直接重写的,实际就里面增加了两句,具体忘了。
源码:
package com.isd.dig.util;
/*jadclipse*/// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.hibernate.Hibernate;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.function.NoArgSQLFunction;
import org.hibernate.dialect.function.StandardSQLFunction;
import org.hibernate.util.StringHelper;
// Referenced classes of package org.hibernate.dialect:
// Dialect
//解决hibernate的Dialect问题,使操作MYSQL数据库时候 sql可以使用 createSQLQuery方法查询*andy*
public class MySQLDialect extends Dialect
{
public MySQLDialect()
{
registerColumnType(-7, "bit");
registerColumnType(-5, "bigint");
registerColumnType(5, "smallint");
registerColumnType(-6, "tinyint");
registerColumnType(4, "integer");
registerColumnType(1, "char(1)");
registerColumnType(6, "float");
registerColumnType(8, "double precision");
registerColumnType(91, "date");
registerColumnType(92, "time");
registerColumnType(93, "datetime");
registerColumnType(-3, "longblob");
registerColumnType(-3, 16777215, "mediumblob");
registerColumnType(-3, 65535, "blob");
registerColumnType(-3, 255, "tinyblob");
registerColumnType(2, "numeric($p,$s)");
registerColumnType(2004, "longblob");
registerColumnType(2004, 16777215, "mediumblob");
registerColumnType(2004, 65535, "blob");
registerColumnType(2005, "longtext");
registerColumnType(2005, 16777215, "mediumtext");
registerColumnType(2005, 65535, "text");
registerVarcharTypes();
registerFunction("ascii", new StandardSQLFunction("ascii", Hibernate.INTEGER));
registerFunction("bin", new StandardSQLFunction("bin", Hibernate.STRING));
registerFunction("char_length", new StandardSQLFunction("char_length", Hibernate.LONG));
registerFunction("character_length", new StandardSQLFunction("character_length", Hibernate.LONG));
registerFunction("lcase", new StandardSQLFunction("lcase"));
registerFunction("lower", new StandardSQLFunction("lower"));
registerFunction("length", new StandardSQLFunction("length", Hibernate.LONG));
registerFunction("ltrim", new StandardSQLFunction("ltrim"));
registerFunction("ord", new StandardSQLFunction("ord", Hibernate.INTEGER));
registerFunction("quote", new StandardSQLFunction("quote"));
registerFunction("reverse", new StandardSQLFun