HQL calls a custom function in Hibernate.

Source: Internet
Author: User

When writing the HQL statement, if you use a custom function, HQL will not compile through the compilation process, we need to rewrite the next dialect dialect class, where my Oracle is 10, can inherit different Oracle dialect classes according to their Oracle version.

Steps to resolve the error:

First, rewrite the dialect class, such as Oracle10gdialect

Second, modify hibernate configuration file, the default dialect class, change to our rewritten dialect class

--------------- Database function wx_f_get_partystr Enter number output VARCHAR2------------

CREATE OR REPLACE FUNCTION wx_f_get_partystr (v_dept_id number-the department ID included in the personnel information) RETURN VARCHAR2 as
T_up_dept number (10);--Parent Department ID, temp parameter
T_dept_temp VARCHAR2 (2000);--Department name
T_dept_str VARCHAR2 (2000);--A string of stitched department names to return
BEGIN

Omitted

END Wx_f_get_partystr;

-------------------------------- , rewrite the dialect class, with the function name in lowercase ------------------------------------------------------- --

Import Org.hibernate.dialect.oracle10gdialect;import Org.hibernate.dialect.function.sqlfunctiontemplate;import Org.hibernate.type.stringtype;public class Mydialect extends Oracle10gdialect{public Mydialect () {super ();//function name must be lowercase , test capitalization error//sqlfunctiontemplate function The first argument is the output type of the function, VARCHAR2 corresponds to the new StringType () number    corresponding to the new Integertype ()//?1 represents the first argument, ? 2 for the second parameter     this is the database Wx_f_get_partystr function requires only one parameter, so write Wx_f_get_partystr (? 1) this.registerfunction ("wx_f_get_ Partystr ", New Sqlfunctiontemplate (New StringType ()," Wx_f_get_partystr (? 1) "));}}


------------------------ Second, modify the Hibernate configuration file, modify the service after the completion of the restart ---------------------------------------------

Change the original hibernate.dialect by default from the original hibernate to the dialect class we rewrote

<property name= "Hibernate.dialect" >org.hibernate.dialect.Oracle10gDialect</property> changed to <property Name= "Hibernate.dialect" >com.haiyisoft.wx.web.struts.MyDialect</property>-------------------HQL calls to identify---------------------String hql= "select New Com.haiyisoft.entity.wx.AddressBook (A.userid,a.username," +
"Wx_f_get_partystr (a.department)," +
"A.position, A.mobile,a.email,a.weixinid,a.avatar,a.status)" +
"From Wxcontacts a where A.userid in (" +userid_str+ ") Order by substr (a.username,0,1)";

HQL calls a custom function in Hibernate.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.