0

我们有一个运行 1.3.7 的遗留 grails 应用程序,并且无法映射到具有日期数据类型的 mssql 服务器 2k8 表。

sqlType:'date'当它尝试加载实体 ERROR util.JDBCExceptionReporter - Can't convert '2013-07-24' to Timestamp 时,我已经将它映射到它仍然会出现以下错误。

添加了自定义方言扩展SQLServerDialect与以下没有区别

registerColumnType( Types.DATE, "date" );
4

1 回答 1

0

Grails 1.3.7 与 hibernate 3.3.1 GA 捆绑在一起。我认为SQLServer2008Dialect这个版本的 Hibernate 中没有,只有一个适用于 2k 和 2k5 版本的`SQLServerDialect̀

您必须从更高版本中借用此类,如本博文中所述

为了存档,我在这里复制课程(来自 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,上面的代码使用了SQLServer2005Dialect3.3.1 GA 版本中不存在的类。我修改了上面的代码以考虑到这一点(我使用SQLServerDialect了),同时假设这些类是兼容的。我没有检查这个。

于 2013-02-13T21:58:25.057 回答