当前位置:首页 > 开发 > 数据库 > 正文

DB2自定义数据库方言

发表于: 2012-05-23   作者:css_gongxifacai   来源:转载   浏览:
摘要: 在DB2数据库上建了一张表,由于表中有一个字段值可能很大,所以就使用到了Long varchar类型 。使用应用程序对表进行查询操作时,后台出现异常。异常信息: org.springframework.orm.hibernate3.HibernateSystemException: No Dialect mapping for JDBC type: -1。 查了多方面原因,最后确定是由于查询结
在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则更适合于第二种方式。

DB2自定义数据库方言

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号