设为首页 收藏本站
查看: 1196|回复: 0

[经验分享] DB2自定义数据库方言

[复制链接]

尚未签到

发表于 2016-11-16 04:09:51 | 显示全部楼层 |阅读模式
在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则更适合于第二种方式。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-300791-1-1.html 上篇帖子: DB2的olap函数详解(原创) 下篇帖子: [Tips] 跨平台备份/恢复DB2数据库
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表