`

自定义hibernate方言,新增自定义函数

    博客分类:
  • java
阅读更多
按位与运算(&)在许多数据库中都是支持的,遗憾的是,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.QueryException;
import org.hibernate.dialect.function.SQLFunction;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.type.StandardBasicTypes;
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 StandardBasicTypes.INTEGER;
}

public boolean hasArguments() {
   return true;
}

public boolean hasParenthesesIfNoArguments() {
   return true;
}

    @Override
    public String render(Type firstArgumentType, List arguments, SessionFactoryImplementor factory) throws QueryException {
        if (arguments.size() != 2) {
            throw new IllegalArgumentException("BitAndFunction requires 2 arguments!");
        }
        return arguments.get(0).toString() + " & " + arguments.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替换配置文件中的设置,

CustomSQLDialect  这个是注册自定义function用的,
在hibernate.xml 的prop里面配置。
例如<prop key="hibernate.dialect">com....CustomSQLDialect</prop>
中间换成你的路径

然后修改HQL:

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


注:转自 http://hi.baidu.com/sushangzhou/blog/item/0f743bfa5a2ffb1f6d22eb9d.html


在配置hibernate.cfg.xml时需指定使用数据库的方言:
例:
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
以下是各数据库对应的方言(Dialect):
数据库
方言(Dialect)
DB2
org.hibernate.dialect.DB2Dialect
DB2 AS/400
org.hibernate.dialect.DB2400Dialect
DB2 OS390
org.hibernate.dialect.DB2390Dialect
PostgreSQL
org.hibernate.dialect.PostgreSQLDialect
MySQL5
org.hibernate.dialect.MySQL5Dialect
MySQL5 with InnoDB
org.hibernate.dialect.MySQL5InnoDBDialect
MySQL with MyISAM
org.hibernate.dialect.MySQLMyISAMDialect
Oracle(any version)
org.hibernate.dialect.OracleDialect
Oracle 9i
org.hibernate.dialect.Oracle9iDialect
Oracle 10g
org.hibernate.dialect.Oracle10gDialect
Oracle 11g
org.hibernate.dialect.Oracle10gDialect
Sybase
org.hibernate.dialect.SybaseASE15Dialect
Sybase Anywhere
org.hibernate.dialect.SybaseAnywhereDialect
Microsoft SQL Server 2000
org.hibernate.dialect.SQLServerDialect
Microsoft SQL Server 2005
org.hibernate.dialect.SQLServer2005Dialect
Microsoft SQL Server 2008
org.hibernate.dialect.SQLServer2008Dialect
SAP DB
org.hibernate.dialect.SAPDBDialect
Informix
org.hibernate.dialect.InformixDialect
HypersonicSQL
org.hibernate.dialect.HSQLDialect
H2 Database
org.hibernate.dialect.H2Dialect
Ingres
org.hibernate.dialect.IngresDialect
Progress
org.hibernate.dialect.ProgressDialect
Mckoi SQL
org.hibernate.dialect.MckoiDialect
Interbase
org.hibernate.dialect.InterbaseDialect
Pointbase
org.hibernate.dialect.PointbaseDialect
FrontBase
org.hibernate.dialect.FrontbaseDialect
Firebird
org.hibernate.dialect.FirebirdDialect

转载请注明出处:http://blog.csdn.net/jialinqiang/article/details/8679171
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics