`
mixer_a
  • 浏览: 344291 次
社区版块
存档分类
最新评论

HQL调用自定义函数

 
阅读更多
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;
转自:http://luveelin.blog.163.com/blog/static/11949234120123154450651/
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics