在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自定义数据库方言相关推荐

  1. 重写了mysql5dialect_自定义一个方言类——Hibernate Dialect

    该类需要继承与我们使用的数据库相应的方言类.比如:如果我们用的是MySql(版本为5.x.x),我们需要继承"org.hibernate.dialect.MySQL5Dialect" ...

  2. Hibernate中的数据库方言(Dialect)

    Hibernate中的数据库方言(Dialect) 在配置hibernate.cfg.xml时需指定使用数据库的方言: 例: <property name="dialect" ...

  3. 金蝶EAS,KSQL,执行数据库方言

    执行数据库方言时,在数据库脚本前添加方言标记:/*dialect*/ SQL语句编写遵循KSQL使用指南,如需使用特定数据库方言,请注意以下规则: 无论在查询分析器还是代码中,使用SQL方言时,在SQ ...

  4. 数据库方言(Dialect)

    在配置hibernate.cfg.xml时需指定使用数据库的方言: 例:<property name="dialect">org.hibernate.dialect.M ...

  5. DB 查询分析器 方便地创建DB2自定义函数

    DB 查询分析器 方便地创建DB2自定义函数                            马根峰             (广东联合电子服务股份有限公司, 广州 510300) 摘要     ...

  6. Hibernate数据库方言

    前言 在部署某个项目代码时,使用maven clean package wildfly:deploy命令部署到wild服务器时总是报错(报错信息暂时无法提供了).后经查询,是Hibernate方言的问 ...

  7. 什么是数据库方言?为什么要配置数据库方言?

    好久没更新博客了,这段时间都在写P8文档,主要就是开发手册和指南之类的.上周刚刚写完,其中一个小伙伴看了这个文档,问了一个很有意思的问题,我觉得有必要拿出来说一下.他问什么是数据库方言? 说这个问题之 ...

  8. DB2自定义函数(C语言)

    1      总体介绍 基于DB2 V9.5 1.1    用户自定义函数(UDF) DB2 内部提供了大量的函数,但仍然不足以满足业务千奇百怪的需要.不过DB2提供了用户自定义函数(User Def ...

  9. 数据库方言Dialect

    使用Hibernate,在配置hibernate.cfg.xml时需指定使用数据库的方言: 例: <property name="dialect">org.hibern ...

最新文章

  1. 附加 集合数据_浩辰3D软件新手教程:三维建模设计中如何重用CAD模型数据?
  2. 斯坦福大学开发自主无人机摄影的算法,可显著提高电影中无人机镜头品质
  3. 实现Redis用户会话 - 1
  4. 【渝粤题库】广东开放大学 综合英语1 形成性考核 (2)
  5. python装饰器class_PYTHON里的装饰器能装饰类吗
  6. pandas重采样时指定对不同列的不同操作
  7. LeetCode算法题-Number Complement(Java实现-五种解法)
  8. 数据分析|如何利用BI工具,探索各商品的潜在关联价值
  9. mysqlbinlog结合sed命令恢复update时未加where条件之前的数据
  10. AWVS12 防止反复注册
  11. 遥感原理与应用 【I】
  12. 励志:滴滴打车App初期是怎么推广的?
  13. 520特别企划 | Z世代进入婚恋市场,婚恋交友行业会有什么变化?​
  14. python学习 之 pyqt5前后端分离试验(进度条)
  15. 紫晶存储研发核心成员离职,不受影响是真的么?
  16. python 图像识别游戏_基于Python的浏览器图像识别
  17. 2022-2027年中国金融服务业RFID行业发展前景及投资战略咨询报告
  18. 第三届上海大学生网络安全大赛 - 登机牌WP
  19. java获取gps 串口_从串口读取GPS数据
  20. html前世今生以及与css和js的关系

热门文章

  1. R语言的帮助使用和图形功能简介
  2. 【恩墨学院】京东618大促网关承载十亿调用量背后的架构实践
  3. 练习print函数的使用(python)
  4. java爬取王者荣耀全皮肤图片
  5. HTML静态网页作业——我的家乡安庆
  6. 简书PC端私密文章放在哪?
  7. 混杂模式和非混杂模式
  8. 波特、码元与比特的关系
  9. 静静的活不埋怨也不嘲笑
  10. java 浮雕效果_android 图像处理(黑白,模糊,浮雕,圆角,镜像,底片,油画,灰白,加旧,哈哈镜,放大镜)...