hibernate mysql自定义变量_hibernate如何得到mysql的变量
你這執行的是純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 StandardSQLFunction("reverse"));
registerFunction("rtrim", new StandardSQLFunction("rtrim"));
registerFunction("soundex", new StandardSQLFunction("soundex"));
registerFunction("space", new StandardSQLFunction("space", Hibernate.STRING));
registerFunction("ucase", new StandardSQLFunction("ucase"));
registerFunction("upper", new StandardSQLFunction("upper"));
registerFunction("unhex", new StandardSQLFunction("unhex", Hibernate.STRING));
registerFunction("abs", new StandardSQLFunction("abs"));
registerFunction("sign", new StandardSQLFunction("sign", Hibernate.INTEGER));
registerFunction("acos", new StandardSQLFunction("acos", Hibernate.DOUBLE));
registerFunction("asin", new StandardSQLFunction("asin", Hibernate.DOUBLE));
registerFunction("atan", new StandardSQLFunction("atan", Hibernate.DOUBLE));
registerFunction("cos", new StandardSQLFunction("cos", Hibernate.DOUBLE));
registerFunction("cot", new StandardSQLFunction("cot", Hibernate.DOUBLE));
registerFunction("crc32", new StandardSQLFunction("crc32", Hibernate.LONG));
registerFunction("exp", new StandardSQLFunction("exp", Hibernate.DOUBLE));
registerFunction("ln", new StandardSQLFunction("ln", Hibernate.DOUBLE));
registerFunction("log", new StandardSQLFunction("log", Hibernate.DOUBLE));
registerFunction("log2", new StandardSQLFunction("log2", Hibernate.DOUBLE));
registerFunction("log10", new StandardSQLFunction("log10", Hibernate.DOUBLE));
registerFunction("pi", new NoArgSQLFunction("pi", Hibernate.DOUBLE));
registerFunction("rand", new NoArgSQLFunction("rand", Hibernate.DOUBLE));
registerFunction("sin", new StandardSQLFunction("sin", Hibernate.DOUBLE));
registerFunction("sqrt", new StandardSQLFunction("sqrt", Hibernate.DOUBLE));
registerFunction("tan", new StandardSQLFunction("tan", Hibernate.DOUBLE));
registerFunction("radians", new StandardSQLFunction("radians", Hibernate.DOUBLE));
registerFunction("degrees", new StandardSQLFunction("degrees", Hibernate.DOUBLE));
registerFunction("ceiling", new StandardSQLFunction("ceiling", Hibernate.INTEGER));
registerFunction("ceil", new StandardSQLFunction("ceil", Hibernate.INTEGER));
registerFunction("floor", new StandardSQLFunction("floor", Hibernate.INTEGER));
registerFunction("round", new StandardSQLFunction("round", Hibernate.INTEGER));
registerFunction("datediff", new StandardSQLFunction("datediff", Hibernate.INTEGER));
registerFunction("timediff", new StandardSQLFunction("timediff", Hibernate.TIME));
registerFunction("date_format", new StandardSQLFunction("date_format", Hibernate.STRING));
registerFunction("curdate", new NoArgSQLFunction("curdate", Hibernate.DATE));
registerFunction("curtime", new NoArgSQLFunction("curtime", Hibernate.TIME));
registerFunction("current_date", new NoArgSQLFunction("current_date", Hibernate.DATE, false));
registerFunction("current_time", new NoArgSQLFunction("current_time", Hibernate.TIME, false));
registerFunction("current_timestamp", new NoArgSQLFunction("current_timestamp", Hibernate.TIMESTAMP, false));
registerFunction("date", new StandardSQLFunction("date", Hibernate.DATE));
registerFunction("day", new StandardSQLFunction("day", Hibernate.INTEGER));
registerFunction("dayofmonth", new StandardSQLFunction("dayofmonth", Hibernate.INTEGER));
registerFunction("dayname", new StandardSQLFunction("dayname", Hibernate.STRING));
registerFunction("dayofweek", new StandardSQLFunction("dayofweek", Hibernate.INTEGER));
registerFunction("dayofyear", new StandardSQLFunction("dayofyear", Hibernate.INTEGER));
registerFunction("from_days", new StandardSQLFunction("from_days", Hibernate.DATE));
registerFunction("from_unixtime", new StandardSQLFunction("from_unixtime", Hibernate.TIMESTAMP));
registerFunction("hour", new StandardSQLFunction("hour", Hibernate.INTEGER));
registerFunction("last_day", new StandardSQLFunction("last_day", Hibernate.DATE));
registerFunction("localtime", new NoArgSQLFunction("localtime", Hibernate.TIMESTAMP));
registerFunction("localtimestamp", new NoArgSQLFunction("localtimestamp", Hibernate.TIMESTAMP));
registerFunction("microseconds", new StandardSQLFunction("microseconds", Hibernate.INTEGER));
registerFunction("minute", new StandardSQLFunction("minute", Hibernate.INTEGER));
registerFunction("month", new StandardSQLFunction("month", Hibernate.INTEGER));
registerFunction("monthname", new StandardSQLFunction("monthname", Hibernate.STRING));
registerFunction("now", new NoArgSQLFunction("now", Hibernate.TIMESTAMP));
registerFunction("quarter", new StandardSQLFunction("quarter", Hibernate.INTEGER));
registerFunction("second", new StandardSQLFunction("second", Hibernate.INTEGER));
registerFunction("sec_to_time", new StandardSQLFunction("sec_to_time", Hibernate.TIME));
registerFunction("sysdate", new NoArgSQLFunction("sysdate", Hibernate.TIMESTAMP));
registerFunction("time", new StandardSQLFunction("time", Hibernate.TIME));
registerFunction("timestamp", new StandardSQLFunction("timestamp", Hibernate.TIMESTAMP));
registerFunction("time_to_sec", new StandardSQLFunction("time_to_sec", Hibernate.INTEGER));
registerFunction("to_days", new StandardSQLFunction("to_days", Hibernate.LONG));
registerFunction("unix_timestamp", new StandardSQLFunction("unix_timestamp", Hibernate.LONG));
registerFunction("utc_date", new NoArgSQLFunction("utc_date", Hibernate.STRING));
registerFunction("utc_time", new NoArgSQLFunction("utc_time", Hibernate.STRING));
registerFunction("utc_timestamp", new NoArgSQLFunction("utc_timestamp", Hibernate.STRING));
registerFunction("week", new StandardSQLFunction("week", Hibernate.INTEGER));
registerFunction("weekday", new StandardSQLFunction("weekday", Hibernate.INTEGER));
registerFunction("weekofyear", new StandardSQLFunction("weekofyear", Hibernate.INTEGER));
registerFunction("year", new StandardSQLFunction("year", Hibernate.INTEGER));
registerFunction("yearweek", new StandardSQLFunction("yearweek", Hibernate.INTEGER));
registerFunction("hex", new StandardSQLFunction("hex", Hibernate.STRING));
registerFunction("oct", new StandardSQLFunction("oct", Hibernate.STRING));
registerFunction("octet_length", new StandardSQLFunction("octet_length", Hibernate.LONG));
registerFunction("bit_length", new StandardSQLFunction("bit_length", Hibernate.LONG));
registerFunction("bit_count", new StandardSQLFunction("bit_count", Hibernate.LONG));
registerFunction("encrypt", new StandardSQLFunction("encrypt", Hibernate.STRING));
registerFunction("md5", new StandardSQLFunction("md5", Hibernate.STRING));
registerFunction("sha1", new StandardSQLFunction("sha1", Hibernate.STRING));
registerFunction("sha", new StandardSQLFunction("sha", Hibernate.STRING));
registerFunction("concat", new StandardSQLFunction("concat", Hibernate.STRING));
getDefaultProperties().setProperty("hibernate.max_fetch_depth", "2");
getDefaultProperties().setProperty("hibernate.jdbc.batch_size", "15");
}
protected void registerVarcharTypes()
{
registerColumnType(12, "longtext");
registerColumnType(12, 16777215, "mediumtext");
registerColumnType(12, 65535, "text");
registerColumnType(12, 255, "varchar($l)");
registerHibernateType( -1, Hibernate.STRING.getName());
}
public String getAddColumnString()
{
return "add column";
}
public boolean qualifyIndexName()
{
return false;
}
public boolean supportsIdentityColumns()
{
return true;
}
public String getIdentitySelectString()
{
return "select last_insert_id()";
}
public String getIdentityColumnString()
{
return "not null auto_increment";
}
public String getAddForeignKeyConstraintString(String constraintName, String foreignKey[], String referencedTable, String primaryKey[], boolean referencesPrimaryKey)
{
String cols = StringHelper.join(", ", foreignKey);
return (new StringBuffer(30)).append(" add index ").append(constraintName).append(" (").append(cols).append("), add constraint ").append(constraintName).append(" foreign key (").append(cols).append(") references ").append(referencedTable).append(" (").append(StringHelper.join(", ", primaryKey)).append(')').toString();
}
public boolean supportsLimit()
{
return true;
}
public String getDropForeignKeyString()
{
return " drop foreign key ";
}
public String getLimitString(String sql, boolean hasOffset)
{
return (new StringBuffer(sql.length() + 20)).append(sql).append(hasOffset ? " limit ?, ?" : " limit ?").toString();
}
public char closeQuote()
{
return '`';
}
public char openQuote()
{
return '`';
}
public boolean supportsIfExistsBeforeTableName()
{
return true;
}
public String getSelectGUIDString()
{
return "select uuid()";
}
public boolean supportsCascadeDelete()
{
return false;
}
public String getTableComment(String comment)
{
return " comment='" + comment + "'";
}
public String getColumnComment(String comment)
{
return " comment '" + comment + "'";
}
public boolean supportsTemporaryTables()
{
return true;
}
public String getCreateTemporaryTableString()
{
return "create temporary table if not exists";
}
public String getCastTypeName(int code)
{
if(code == 4)
return "signed";
if(code == 12)
return "char";
if(code == -3)
return "binary";
else
return super.getCastTypeName(code);
}
public boolean supportsCurrentTimestampSelection()
{
return true;
}
public boolean isCurrentTimestampSelectStringCallable()
{
return false;
}
public String getCurrentTimestampSelectString()
{
return "select now()";
}
public int registerResultSetOutParameter(CallableStatement statement, int col)
throws SQLException
{
return col;
}
public ResultSet getResultSet(CallableStatement ps)
throws SQLException
{
for(boolean isResultSet = ps.execute(); !isResultSet && ps.getUpdateCount() != -1; isResultSet = ps.getMoreResults());
return ps.getResultSet();
}
public boolean supportsRowValueConstructorSyntax()
{
return true;
}
public Boolean performTemporaryTableDDLInIsolation()
{
return Boolean.FALSE;
}
public boolean supportsEmptyInList()
{
return false;
}
public boolean areStringComparisonsCaseInsensitive()
{
return true;
}
public boolean supportsLobValueChangePropogation()
{
return false;
}
public boolean supportsSubqueryOnMutatingTable()
{
return false;
}
}
將此文件放到本地,再將hibernate配置文件里的方言路徑寫成你本地的,
你本地路徑.MySQLDialect
2010年12月19日 12:09
總結
以上是生活随笔為你收集整理的hibernate mysql自定义变量_hibernate如何得到mysql的变量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos7安装mysql8.0简书_
- 下一篇: mysql事务服务_MySQL (事务篇