Hibernate 自定义方言

Hibernate 自带方言

DB2 org.hibernate.dialect.DB2Dialect
DB2 AS/400 org.hibernate.dialect.DB2400Dialect
DB2 OS390 org.hibernate.dialect.DB2390Dialect
PostgreSQL org.hibernate.dialect.PostgreSQLDialect
MySQL org.hibernate.dialect.MySQLDialect
MySQL5  org.hibernate.dialect.MySQL5Dialect
MySQL with InnoDB org.hibernate.dialect.MySQLInnoDBDialect
MySQL with MyISAM org.hibernate.dialect.MySQLMyISAMDialect
Oracle (any version) org.hibernate.dialect.OracleDialect
Oracle 9i/10g org.hibernate.dialect.Oracle9Dialect
Sybase org.hibernate.dialect.SybaseDialect
Sybase Anywhere org.hibernate.dialect.SybaseAnywhereDialect
Microsoft SQL Server org.hibernate.dialect.SQLServerDialect
SAP DB org.hibernate.dialect.SAPDBDialect
Informix org.hibernate.dialect.InformixDialect
HypersonicSQL org.hibernate.dialect.HSQLDialect
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


1.创建自己的方言
public class DialectRegExp extends Oracle9iDialect{
public DialectRegExp(){
super();
this.registerFunction("my_xy", new SQLFunctionTemplate(new IntegerType(),"get_sal(?1)"));
}
}
?1代表第一个参数,?2代表第二个参数.详情见hibernate javadoc:

org.hibernate.dialect.Dialect,

org.hibernate.dialect.function.SQLFunction,

org.hibernate.dialect.function.SQLFunctionTemplate


2.主配置文件注册自己的方言
<property name="hibernate.dialect">org.xl.DialectRegExp</property>
注意:
<property name="hibernate.query.substitutions">true=1,false=false</property>
这句是为了某些情况在特殊使用,在文档中有解释,意思是是否在hibernate中用1表示true,0表示false

3.调用,此时的my_xy就是自定义函数get_sal
Query q = se.createQuery("select my_xy(e.empno) from Emp e");
List list = q.list();
System.out.println(list.size());


get_sal过程:
create or replace function get_sal(eno number) return number
is
  mysal emp.sal%type;
begin
  if validate_emp(eno) then
   select sal into mysal from emp where empno=eno;
   return mysal;
  end if;
end;

猜你喜欢

转载自sortaxie.iteye.com/blog/1545702