`
css_gongxifacai
  • 浏览: 30921 次
  • 性别: Icon_minigender_2
  • 来自: 湖北
社区版块
存档分类
最新评论

DB2自定义数据库方言

阅读更多
在DB2数据库上建了一张表,由于表中有一个字段值可能很大,所以就使用到了Long varchar类型 。使用应用程序对表进行查询操作时,后台出现异常。异常信息:
org.springframework.orm.hibernate3.HibernateSystemException: No Dialect mapping for JDBC type: -1。
查了多方面原因,最后确定是由于查询结果无法从数据库类型转换为java类型,因为有两种解决方案:
一:将数据库类型转换为常用类型,如varchar
二:自定义方言包,设定某种数据库类型与java类型之间的转换。
在这里具体详解自定义方言包。
其实,自定义方言包没有大家想像中那么难。主要3步即可实现自定义。
1.编写类,继承你的数据库方言包类。
   如:MySql 则org.hibernate.dialect.MySQLxxDialect;
   而我使用的是DB2,则org.hibernate.dialect.DB2Dialect
2.将应用的配置文件中方言包路径都改为自定义方言包
  如:Application*.xml下
      <properties> 
            <property name="hibernate.dialect" value="xxx.xxx.OwnDefineDialect"/> 
            <property name="hibernate.hbm2ddl.auto" value="none" /> 
     </properties>
3.编写你的自定义方言包。
  在这里建议大家看看原方言包的源码。在源码中,构造函数中就对几个常用的数据库数据类型进行了转换。
  如DB2Dialect:
  public DB2Dialect()
  {
    registerColumnType(-7, "smallint");
    registerColumnType(-5, "bigint");
    registerColumnType(5, "smallint");
    registerColumnType(-6, "smallint");
    registerColumnType(4, "integer");
    registerColumnType(1, "char(1)");
    registerColumnType(12, "varchar($l)");
    registerColumnType(6, "float");
    registerColumnType(8, "double");
    registerColumnType(91, "date");
    registerColumnType(92, "time");
    registerColumnType(93, "timestamp");
    registerColumnType(-3, "varchar($l) for bit data");
    registerColumnType(2, "numeric($p,$s)");
    registerColumnType(2004, "blob($l)");
    registerColumnType(2005, "clob($l)");

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

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

    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("degrees", new StandardSQLFunction("degrees", Hibernate.DOUBLE));
    registerFunction("exp", new StandardSQLFunction("exp", Hibernate.DOUBLE));
    registerFunction("float", new StandardSQLFunction("float", Hibernate.DOUBLE));
    registerFunction("hex", new StandardSQLFunction("hex", Hibernate.STRING));
    registerFunction("ln", new StandardSQLFunction("ln", Hibernate.DOUBLE));
    registerFunction("log", new StandardSQLFunction("log", Hibernate.DOUBLE));
    registerFunction("log10", new StandardSQLFunction("log10", Hibernate.DOUBLE));
    registerFunction("radians", new StandardSQLFunction("radians", Hibernate.DOUBLE));
    registerFunction("rand", new NoArgSQLFunction("rand", Hibernate.DOUBLE));
    registerFunction("sin", new StandardSQLFunction("sin", Hibernate.DOUBLE));
    registerFunction("soundex", new StandardSQLFunction("soundex", Hibernate.STRING));
    registerFunction("sqrt", new StandardSQLFunction("sqrt", Hibernate.DOUBLE));
    registerFunction("stddev", new StandardSQLFunction("stddev", Hibernate.DOUBLE));
    registerFunction("tan", new StandardSQLFunction("tan", Hibernate.DOUBLE));
    registerFunction("variance", new StandardSQLFunction("variance", Hibernate.DOUBLE));

    registerFunction("julian_day", new StandardSQLFunction("julian_day", Hibernate.INTEGER));
    registerFunction("microsecond", new StandardSQLFunction("microsecond", Hibernate.INTEGER));
    registerFunction("midnight_seconds", new StandardSQLFunction("midnight_seconds", Hibernate.INTEGER));
    registerFunction("minute", new StandardSQLFunction("minute", Hibernate.INTEGER));
    registerFunction("month", new StandardSQLFunction("month", Hibernate.INTEGER));
    registerFunction("monthname", new StandardSQLFunction("monthname", Hibernate.STRING));
    registerFunction("quarter", new StandardSQLFunction("quarter", Hibernate.INTEGER));
    registerFunction("hour", new StandardSQLFunction("hour", Hibernate.INTEGER));
    registerFunction("second", new StandardSQLFunction("second", Hibernate.INTEGER));
    registerFunction("current_date", new NoArgSQLFunction("current date", Hibernate.DATE, false));
    registerFunction("date", new StandardSQLFunction("date", Hibernate.DATE));
    registerFunction("day", new StandardSQLFunction("day", Hibernate.INTEGER));
    registerFunction("dayname", new StandardSQLFunction("dayname", Hibernate.STRING));
    registerFunction("dayofweek", new StandardSQLFunction("dayofweek", Hibernate.INTEGER));
    registerFunction("dayofweek_iso", new StandardSQLFunction("dayofweek_iso", Hibernate.INTEGER));
    registerFunction("dayofyear", new StandardSQLFunction("dayofyear", Hibernate.INTEGER));
    registerFunction("days", new StandardSQLFunction("days", Hibernate.LONG));
    registerFunction("current_time", new NoArgSQLFunction("current time", Hibernate.TIME, false));
    registerFunction("time", new StandardSQLFunction("time", Hibernate.TIME));
    registerFunction("current_timestamp", new NoArgSQLFunction("current timestamp", Hibernate.TIMESTAMP, false));
    registerFunction("timestamp", new StandardSQLFunction("timestamp", Hibernate.TIMESTAMP));
    registerFunction("timestamp_iso", new StandardSQLFunction("timestamp_iso", Hibernate.TIMESTAMP));
    registerFunction("week", new StandardSQLFunction("week", Hibernate.INTEGER));
    registerFunction("week_iso", new StandardSQLFunction("week_iso", Hibernate.INTEGER));
    registerFunction("year", new StandardSQLFunction("year", Hibernate.INTEGER));

    registerFunction("double", new StandardSQLFunction("double", Hibernate.DOUBLE));
    registerFunction("varchar", new StandardSQLFunction("varchar", Hibernate.STRING));
    registerFunction("real", new StandardSQLFunction("real", Hibernate.FLOAT));
    registerFunction("bigint", new StandardSQLFunction("bigint", Hibernate.LONG));
    registerFunction("char", new StandardSQLFunction("char", Hibernate.CHARACTER));
    registerFunction("integer", new StandardSQLFunction("integer", Hibernate.INTEGER));
    registerFunction("smallint", new StandardSQLFunction("smallint", Hibernate.SHORT));

    registerFunction("digits", new StandardSQLFunction("digits", Hibernate.STRING));
    registerFunction("chr", new StandardSQLFunction("chr", Hibernate.CHARACTER));
    registerFunction("upper", new StandardSQLFunction("upper"));
    registerFunction("lower", new StandardSQLFunction("lower"));
    registerFunction("ucase", new StandardSQLFunction("ucase"));
    registerFunction("lcase", new StandardSQLFunction("lcase"));
    registerFunction("length", new StandardSQLFunction("length", Hibernate.LONG));
    registerFunction("ltrim", new StandardSQLFunction("ltrim"));
    registerFunction("rtrim", new StandardSQLFunction("rtrim"));
    registerFunction("substr", new StandardSQLFunction("substr", Hibernate.STRING));
    registerFunction("posstr", new StandardSQLFunction("posstr", Hibernate.INTEGER));

    registerFunction("substring", new StandardSQLFunction("substr", Hibernate.STRING));
    registerFunction("bit_length", new SQLFunctionTemplate(Hibernate.INTEGER, "length(?1)*8"));
    registerFunction("trim", new AnsiTrimEmulationFunction());

    registerFunction("concat", new VarArgsSQLFunction(Hibernate.STRING, "", "||", ""));

    registerFunction("str", new SQLFunctionTemplate(Hibernate.STRING, "rtrim(char(?1))"));

    registerKeyword("current");
    registerKeyword("date");
    registerKeyword("time");
    registerKeyword("timestamp");
    registerKeyword("fetch");
    registerKeyword("first");
    registerKeyword("rows");
    registerKeyword("only");

    getDefaultProperties().setProperty("hibernate.jdbc.batch_size", "0");
  }
我们在编写自己的方言包时可根据异常信息进行编写。在此,也可提供方言包数据转换异常type信息:
public static final int BIT = -7;
  public static final int TINYINT = -6;
  public static final int SMALLINT = 5;
  public static final int INTEGER = 4;
  public static final int BIGINT = -5;
  public static final int FLOAT = 6;
  public static final int REAL = 7;
  public static final int DOUBLE = 8;
  public static final int NUMERIC = 2;
  public static final int DECIMAL = 3;
  public static final int CHAR = 1;
  public static final int VARCHAR = 12;
  public static final int LONGVARCHAR = -1;
  public static final int DATE = 91;
  public static final int TIME = 92;
  public static final int TIMESTAMP = 93;
  public static final int BINARY = -2;
  public static final int VARBINARY = -3;
  public static final int LONGVARBINARY = -4;
  public static final int NULL = 0;
  public static final int OTHER = 1111;
  public static final int JAVA_OBJECT = 2000;
  public static final int DISTINCT = 2001;
  public static final int STRUCT = 2002;
  public static final int ARRAY = 2003;
  public static final int BLOB = 2004;
  public static final int CLOB = 2005;
  public static final int REF = 2006;
  public static final int DATALINK = 70;
  public static final int BOOLEAN = 16;
  public static final int ROWID = -8;
  public static final int NCHAR = -15;
  public static final int NVARCHAR = -9;
  public static final int LONGNVARCHAR = -16;
  public static final int NCLOB = 2011;
  public static final int SQLXML = 2009;
若异常信息中提示type为-1,则可从上发现-1为LONGVARCHAR ,则只需在自定义方言包中如此设置:
registerHibernateType(Types.LONGNVARCHAR,Hibernate.STRING.getName());
其它雷同。
但大家需要注意的是,有时候这样写貌似没有用,那么大家可以换一种方式去进行定义:
registerHibernateType(-1,"string");
两种方式是一样的,但可能因为数据库驱动不同而有所差异。
DB2Dialect则更适合于第二种方式。
分享到:
评论

相关推荐

    mybatis-sql-dialect

    通过使用SQL方言包,MyBatis能够更好地适应各种数据库,如MySQL、Oracle和DB2,使得在切换数据库时无需对SQL语句进行大量修改。 1. **MyBatis框架概述** MyBatis是一个轻量级的ORM(对象关系映射)框架,它消除了...

    数据库基础与应用辅导8(新).doc

    1. SQL Server是微软公司开发的关系型数据库管理系统,它支持XML数据类型,允许用户自定义函数,并集成了邮件功能。选项D错误,因为SQL Server不支持网状数据模型,它基于关系模型。 2. 对于大型数据库,为了获得...

    access转mysql的软件db2mysql

    5. 触发器和存储过程:如果Access数据库中包含触发器或自定义函数,db2mysql需要将它们转换为MySQL支持的格式。 6. 处理二进制数据:Access数据库可能包含图像、文件或其他二进制数据,转换时需正确处理这些数据...

    oracle数据库介绍.pptx

    Oracle DBMS还支持PL/SQL,这是一种扩展的SQL方言,包含流程控制语句和用户自定义函数,增强了数据库编程能力。 数据库管理员(DBA)是负责数据库的运行、维护和优化的专业人员,他们确保数据库的性能、安全性和...

    H2Database连接配置.pdf

    H2的兼容性非常强,支持多种数据库方言,包括与DB2、Apache Derby、HSQLDB、MSSQL Server、MySQL、Oracle以及PostgreSQL数据库的兼容性。它还具备了SQL注入问题的解决方案,使得应用更加安全。 H2数据库支持集群,...

    Hibernate主键类型说明和配置手册.doc

    不适用于JTA环境或自定义数据库连接。 7. **seqhilo**: 类似于hilo,但使用数据库的序列,适用于支持序列的数据库。 8. **uuid.string**: 与uuid.hex类似,但生成16位的字符串,不适用于PostgreSQL。 9. **native...

    mybatis pageHelper

    3. **多数据库支持**:PageHelper支持多种主流数据库,包括MySQL、Oracle、DB2、SQL Server、H2等,只需配置相应的数据库方言即可。 4. **灵活的API接口**:PageHelper提供了丰富的API接口,如`Page`对象、`...

    magic-api:magic-api是一个接口快速开发框架,通过Web页编写脚本以及配置,自动映射为HTTP接口,无需定义控制器,服务,Dao,Mapper,XML,VO等Java对象

    支持SQL拦截,自定义分页方言,自定义列名转换 支持自定义JSON结果,自定义分页结果 支持对接口权限配置,拦截器等功能 支持运行时动态修改数据源 支持Swagger接口文档生成 基于引擎,动态编译,无需重启,实时发布 ...

    HBM2JAVA 小工具

    在提供的压缩包文件中,"GenHbmTool"可能是HBM2JAVA的实现或扩展工具,可能包含特定的配置选项或者增强功能,例如自定义模板、支持更多的数据库方言等。使用这个工具时,需要阅读其文档或帮助信息来了解具体的使用...

    hibernate

    - **跨数据库兼容性**:Hibernate支持多种关系型数据库,如DB2、HSQLDB、Oracle、MySQL、SQL Server等,甚至可以扩展自定义的方言类来适应特定的数据库需求。 - **巨大的特性列表**:虽然具体特性未在文档中详细列出...

    SQL 语法提示工具SQL Assistan中文版

    虽然“SQL Assistant”这个名字可能让人误以为它仅适用于某种特定的SQL方言,但实际上,它通常会支持多种常见的数据库管理系统,如MySQL、Oracle、SQL Server、DB2等,确保了跨平台的兼容性和广泛的应用范围。...

    mybatis分页插件pagehelper:jar:3.4.2-fix.jar包

    3. **多数据库支持**:PageHelper 兼容多种主流数据库,包括 MySQL、Oracle、SQL Server、DB2、H2 等,使得在不同数据库间切换变得更加便捷。 4. **参数处理**:PageHelper 自动处理分页参数,如 `offset` 和 `...

    sqltoy-orm框架系统-其他

    5、跨数据库函数方言替换,如:isnull/ifnull/nvl、substr/substring 等不同数据库。 sqltoy-orm特点: 1、最优雅直观的sql编写模式 2、天然防止sql注入,执行过程 3、最强大的分页查询 4、最巧妙的缓存应用,将多表...

    JFinal 最新2.2版本的开发手册

    - **功能**:支持多种数据库方言。 - **示例**: ```java Dialect dialect = new MySQLDialect(); ``` **6.9 表关联操作** - **功能**:支持一对多、多对多等多种关联查询。 - **示例**: ```java List...

    mybatis分页插件PageHelper的使用

    PageHelper是一款为MyBatis设计的分页插件,它实现了包括Oracle、MySQL、MariaDB、SQLite、PostgreSQL、DB2等在内的多种数据库的分页功能。PageHelper通过拦截器机制,自动完成SQL的分页,无需手动编写复杂的分页...

Global site tag (gtag.js) - Google Analytics