2013-02-13 62 views
0

我們有一個傳統的Grails應用程序運行1.3.7和遇到問題映射到MSSQL服務器2K8表日期數據類型。Grails的1.3.7映射到SQL Server的2K8日期列

我找到了與sqlType:'date'映射它仍然得到以下錯誤,當它試圖加載實體錯誤util.JDBCExceptionReporter - 無法將「2013年7月24日」到時間戳。

增加了一個自定義的方言與延長SQLServerDialect下沒有區別

registerColumnType(Types.DATE, "date"); 

回答

0

的Grails 1.3.7帶有休眠3.3.1 GA束。我認爲在這個版本的Hibernate沒有SQLServer2008Dialect,只有一個`SQLServerDialect這是良好的

2K和2K5版本

你必須從一個更高版本借用這個類,如this blog post

我解釋這裏重現類歸檔的緣故(從robert-reiz.com)

import org.hibernate.Hibernate; 
import org.hibernate.cfg.Environment; 
import org.hibernate.dialect.SQLServerDialect; 
import org.hibernate.dialect.function.*; 
import org.hibernate.type.StandardBasicTypes; 

import java.sql.Types; 

public class SqlServer2008Dialect extends SQLServerDialect { 

public SqlServer2008Dialect(){ 
super(); 

registerColumnType(Types.DATE, "date"); 
registerColumnType(Types.TIME, "time"); 
registerColumnType(Types.TIMESTAMP, "datetime2"); 

registerFunction("current_timestamp", new NoArgSQLFunction("current_timestamp",  Hibernate.TIMESTAMP,false)); 

registerColumnType(Types.BIT, "tinyint"); //Sybase BIT type does not support null values 
registerColumnType(Types.BIGINT, "bigint");//changed 
registerColumnType(Types.SMALLINT, "smallint"); 
registerColumnType(Types.TINYINT, "tinyint"); 
registerColumnType(Types.INTEGER, "int"); 
registerColumnType(Types.CHAR, "char(1)"); 
registerColumnType(Types.VARCHAR, "varchar($l)"); 
registerColumnType(Types.FLOAT, "float"); 
registerColumnType(Types.DOUBLE, "double precision"); 

registerColumnType(Types.VARBINARY, "varbinary($l)"); 
registerColumnType(Types.NUMERIC, "numeric($p,$s)"); 
registerColumnType(Types.BLOB, "image"); 
registerColumnType(Types.CLOB, "text"); 
registerColumnType(Types.ROWID, "bigint"); 

registerFunction("ascii", new StandardSQLFunction("ascii", StandardBasicTypes.INTEGER)); 
registerFunction("char", new StandardSQLFunction("char", StandardBasicTypes.CHARACTER)); 
registerFunction("len", new StandardSQLFunction("len", StandardBasicTypes.LONG)); 
registerFunction("lower", new StandardSQLFunction("lower")); 
registerFunction("upper", new StandardSQLFunction("upper")); 
registerFunction("str", new StandardSQLFunction("str", StandardBasicTypes.STRING)); 
registerFunction("ltrim", new StandardSQLFunction("ltrim")); 
registerFunction("rtrim", new StandardSQLFunction("rtrim")); 
registerFunction("reverse", new StandardSQLFunction("reverse")); 
registerFunction("space", new StandardSQLFunction("space", StandardBasicTypes.STRING)); 

registerFunction("user", new NoArgSQLFunction("user", StandardBasicTypes.STRING)); 

registerFunction("current_timestamp", new NoArgSQLFunction("getdate", StandardBasicTypes.TIMESTAMP)); 
registerFunction("current_time", new NoArgSQLFunction("getdate", StandardBasicTypes. TIME)); 
registerFunction("current_date", new NoArgSQLFunction("getdate", StandardBasicTypes. DATE)); 

registerFunction("getdate", new NoArgSQLFunction("getdate", StandardBasicTypes. TIMESTAMP)); 
registerFunction("getutcdate", new NoArgSQLFunction("getutcdate", StandardBasicTypes. TIMESTAMP)); 
registerFunction("day", new StandardSQLFunction("day", StandardBasicTypes.INTEGER)); 
registerFunction("month", new StandardSQLFunction("month", StandardBasicTypes.INTEGER)); 
registerFunction("year", new StandardSQLFunction("year", StandardBasicTypes.INTEGER)) ; 
registerFunction("datename", new StandardSQLFunction("datename", StandardBasicTypes. STRING)); 

registerFunction("abs", new StandardSQLFunction("abs")); 
registerFunction("sign", new StandardSQLFunction("sign", StandardBasicTypes.INTEGER)) ; 

registerFunction("acos", new StandardSQLFunction("acos", StandardBasicTypes.DOUBLE)); 
registerFunction("asin", new StandardSQLFunction("asin", StandardBasicTypes.DOUBLE)); 
registerFunction("atan", new StandardSQLFunction("atan", StandardBasicTypes.DOUBLE)); 
registerFunction("cos", new StandardSQLFunction("cos", StandardBasicTypes.DOUBLE)); 
registerFunction("cot", new StandardSQLFunction("cot", StandardBasicTypes.DOUBLE)); 
registerFunction("exp", new StandardSQLFunction("exp", StandardBasicTypes.DOUBLE)); 
registerFunction("log", new StandardSQLFunction("log", StandardBasicTypes.DOUBLE)); 
registerFunction("log10", new StandardSQLFunction("log10", StandardBasicTypes.DOUBLE)); 
registerFunction("sin", new StandardSQLFunction("sin", StandardBasicTypes.DOUBLE)); 
registerFunction("sqrt", new StandardSQLFunction("sqrt", StandardBasicTypes.DOUBLE)); 
registerFunction("tan", new StandardSQLFunction("tan", StandardBasicTypes.DOUBLE)); 
registerFunction("pi", new NoArgSQLFunction("pi", StandardBasicTypes.DOUBLE)); 
registerFunction("square", new StandardSQLFunction("square")); 
registerFunction("rand", new StandardSQLFunction("rand", StandardBasicTypes.FLOAT)); 

registerFunction("radians", new StandardSQLFunction("radians", StandardBasicTypes. DOUBLE)); 
registerFunction("degrees", new StandardSQLFunction("degrees", StandardBasicTypes. DOUBLE)); 

registerFunction("round", new StandardSQLFunction("round")); 
registerFunction("ceiling", new StandardSQLFunction("ceiling")); 
registerFunction("floor", new StandardSQLFunction("floor")); 

registerFunction("isnull", new StandardSQLFunction("isnull")); 

registerFunction("concat", new VarArgsSQLFunction(StandardBasicTypes.STRING, "( ","+",")")); 

registerFunction("length", new StandardSQLFunction("len", StandardBasicTypes.INTEGER )); 
registerFunction("trim", new SQLFunctionTemplate(StandardBasicTypes.STRING, "ltrim( rtrim(?1))")); 
registerFunction("locate", new CharIndexFunction()); 

getDefaultProperties().setProperty(Environment.STATEMENT_BATCH_SIZE, NO_BATCH); 
} 

} 

你必須添加

dialect = org.hibernate.dialect.SqlServer2008Dialect 

在Datasource.groovy`配置文件中,並使該類可用於您的類路徑

警告:在博客文章中,Hibernate的基礎版本是3.6.4,上面的代碼使用了不存在的SQLServer2005Dialect類在3.3.1 GA版本中。我修改上面的代碼(I使用SQLServerDialect),同時假設這些類是兼容機到考慮到這一點。我沒有檢查這個。

+0

嗯,似乎無法找到這些類的一半'SQLServer2008Dialect.java:89:找不到符號' – dstarh 2013-02-13 22:05:57

+0

是使用您的更新版本,仍然'SQLServer2008Dialect.java:90:找不到符號:變量StandardBasicTypes' – dstarh 2013-02-13 22:09:19