`

【转】解决Hibernate 3不支持 "&" 运算的SQL语句

 
阅读更多
转:http://blog.csdn.net/explorering/article/details/1196397

按位与运算(&)在许多数据库中都是支持的,遗憾的是,Hibernate 3在HQL中不支持&运算,如果你写了如下的HQL:

where a.id & :mask = :target

则Hibernate报错:exception: unexpected char: '&'.

如何解决此问题?方法是利用Hibernate支持的自定义SQLFunction,定义一个bitand(a,b)的SQLFunction,然后,自己写一个解释器,生成a & b的SQL语句。

要实现一个自定义的SQLFunction,必须实现SQLFunction接口:

package com.js.dialect;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.QueryException;
import org.hibernate.dialect.function.SQLFunction;
import org.hibernate.engine.Mapping;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.type.Type;

/**
* <p>
* Title:BitAndFunction
* </p>
* <p>
* Description:
* </p>
*
* @author js
* @version
* @since
*/

public class BitAndFunction implements SQLFunction {
public Type getReturnType(Type type, Mapping mapping) {
  return Hibernate.INTEGER;
}

public boolean hasArguments() {
  return true;
}

public boolean hasParenthesesIfNoArguments() {
  return true;
}

public String render(List args, SessionFactoryImplementor factory)
   throws QueryException {
  if (args.size() != 2) {
   throw new IllegalArgumentException(
     "BitAndFunction requires 2 arguments!");
  }
  return args.get(0).toString() + " & " + args.get(1).toString();
}

}

然后,根据使用的数据库方言,派生一个自定义的CustomSQLDialect:

package com.js.dialect;

import org.hibernate.dialect.MySQLInnoDBDialect;

/**
* <p>
* Title:CustomSQLDialect
* </p>
* <p>
* Description:
* </p>
*
* @author js
* @version
* @since
*/
public class CustomSQLDialect extends MySQLInnoDBDialect {
/**
  *
  */
public CustomSQLDialect() {
  super();
  registerFunction("bitand", new BitAndFunction());
}

}

设定函数名为bitand,参数和返回值均为Hibernate.LONG,现在,用CustomSQLDialect替换配置文件中的设置,然后修改HQL:

where bitand(a.id, :mask) = :target

编译,运行,观察Hibernate的SQL输出,执行不成功!不认识"bitand"!
分享到:
评论
2 楼 asialee 2012-12-03  

希望大家以后注册自定义函数的时候,看看该数据库是否已经实现了该自定义的函数了,希望这个对大家有帮助。
1 楼 asialee 2012-12-03  

    今天遇到一个问题,发现mysql的可以注册,但是H2数据库的死活不行,后来经过调试和分析代码发现,H2数据库天生支持bitand,代码在org.hibernate.dialect.H2Dialect

registerFunction( "acos", new StandardSQLFunction( "acos", Hibernate.DOUBLE ) );
		registerFunction( "asin", new StandardSQLFunction( "asin", Hibernate.DOUBLE ) );
		registerFunction( "atan", new StandardSQLFunction( "atan", Hibernate.DOUBLE ) );
		registerFunction( "atan2", new StandardSQLFunction( "atan2", Hibernate.DOUBLE ) );
		registerFunction( "bitand", new StandardSQLFunction( "bitand", Hibernate.INTEGER ) );
		registerFunction( "bitor", new StandardSQLFunction( "bitor", Hibernate.INTEGER ) );
		registerFunction( "bitxor", new StandardSQLFunction( "bitxor", Hibernate.INTEGER ) );

相关推荐

    精通 Hibernate:Java 对象持久化技术详解(第2版).part2

    第3章 第一个Hibernate应用  3.1 创建Hibernate的配置文件  3.2 创建持久化类  3.3 创建数据库Schema  3.4 创建对象-关系映射文件  3.4.1 映射文件的文档类型定义(DTD)  3.4.2 把Customer持久化类映射到...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part3

    第3章 第一个Hibernate应用  3.1 创建Hibernate的配置文件  3.2 创建持久化类  3.3 创建数据库Schema  3.4 创建对象-关系映射文件  3.4.1 映射文件的文档类型定义(DTD)  3.4.2 把Customer持久化类映射到...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part4

    第3章 第一个Hibernate应用  3.1 创建Hibernate的配置文件  3.2 创建持久化类  3.3 创建数据库Schema  3.4 创建对象-关系映射文件  3.4.1 映射文件的文档类型定义(DTD)  3.4.2 把Customer持久化类映射到...

    精通 Hibernate:Java 对象持久化技术详解(第2版).part1.rar

    第3章 第一个Hibernate应用  3.1 创建Hibernate的配置文件  3.2 创建持久化类  3.3 创建数据库Schema  3.4 创建对象-关系映射文件  3.4.1 映射文件的文档类型定义(DTD)  3.4.2 把Customer持久化类映射到...

    整理后java开发全套达内学习笔记(含练习)

    exist 存在, 发生 [ig'zist] '(SQL关键字 exists) extends (关键字) 继承、扩展 [ik'stend] false (关键字) final (关键字) finally (关键字) fragments 段落; 代码块 ['frægmәnt] FrameWork [java] 结构,...

    Spring中文帮助文档

    11.2.6. 执行SQL语句 11.2.7. 执行查询 11.2.8. 更新数据库 11.2.9. 获取自动生成的主键 11.3. 控制数据库连接 11.3.1. DataSourceUtils类 11.3.2. SmartDataSource接口 11.3.3. AbstractDataSource类 ...

    Spring API

    11.2.6. 执行SQL语句 11.2.7. 执行查询 11.2.8. 更新数据库 11.2.9. 获取自动生成的主键 11.3. 控制数据库连接 11.3.1. DataSourceUtils类 11.3.2. SmartDataSource接口 11.3.3. AbstractDataSource类 ...

    Java学习笔记-个人整理的

    {12.13}DML语句}{175}{section.12.13} {12.13.1}insert}{175}{subsection.12.13.1} {12.13.2}create}{175}{subsection.12.13.2} {12.13.3}rownum}{175}{subsection.12.13.3} {12.13.4}update}{176}{subsection....

Global site tag (gtag.js) - Google Analytics