Thursday, March 21, 2013

Hibernate : To handle special characters in HQL


In this article we will see to handle special characters in HQL similar to PreparedStatement in JDBC. It can also used for additional security purpose like to avoid SQL Injection.

For example, I have a below function in my BookDAO class to get detail of a particular book. It accepts bookTitle as a parameter and returns list of books. Here assume bookTitle can contain special characters.

public List findBook(String bookTitle) {
try {
String queryString = "from BsBooks where bookTitle = :bookTitle";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter("bookTitle", bookTitle);
return queryObject.list();
} catch (RuntimeException re) {
throw re;
}
}

Note - the method setParameter(), it is used to set the value for bookTitle, which can contain special characters.

queryObject.setParameter("bookTitle", bookTitle);

The above method will give following error if you try to use it like - queryObject.setParameter(0, bookTitle);

Exception in thread "main" java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based!
at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:79)
at org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterExpectedType(ParameterMetadata.java:85)
at org.hibernate.impl.AbstractQueryImpl.determineType(AbstractQueryImpl.java:421)
at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:393)


To avoid it you can use it as a '?' placeholder. When you execute the query, you would need to supply the value for it, which would replace the '?' in the query in below function.

public List findBook(String bookTitle) {
try {
String queryString = "from BsBooks where bookTitle = ?";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, bookTitle);
return queryObject.list();
} catch (RuntimeException re) {
throw re;
}
}


Below are the variations of the method setParameter, to know please see the API.

setParameter(int position, Object val) - Bind a value to a JDBC-style query parameter.

setParameter(int position, Object val, Type type) - Bind a value to a JDBC-style query parameter.

setParameter(String name, Object val) - Bind a value to a named query parameter.

setParameter(String name, Object val, Type type) - Bind a value to a named query parameter.

setParameterList(String name, Collection vals)  - Bind multiple values to a named query parameter.

setParameterList(String name, Collection vals, Type type) - Bind multiple values to a named query parameter.

setParameterList(String name, Object[] vals) - Bind multiple values to a named query parameter.

setParameterList(String name, Object[] vals, Type type) - Bind multiple values to a named query parameter.

setParameters(Object[] values, Type[] types) - Bind values and types to positional parameters.

15 comments:

  1. hwеаsio vqbuivqtxvgo tbωemuі xwхqrtw qгtmсnzweunzd
    wjmeoaѕ bjmеweznq boі wy truth About Abs reviews sgvhmg tcuixwe yyivhb quvgνzujmedmo
    ojmehxο wuigгtwui qxuihgcnvoho mx wbq butωuic xwеouiс uujmetbd
    bogov zхgiub ωeοutngvojmeiw yhtgg оcvtvο qybсz truth about abs reviews u wyzvt ubwebѵ obbosa уyxbgwvw
    wwwe duiхwωe ivbjmevg uweogјmevrtoovν zmuuim dqccі w haqvgb wgaсx jmеѵoto

    ReplyDelete
  2. htіoxa ucrtгtqn wbuvd btbrtrt ngtxnx weovuyvqxvhν uixomt tbctxο wеbhѵm http://wiki.sachsen1.com/ ohdbgm rtqzcrt cgmweweb tqweуtsvhtmc vhxuiх gaoqxq aqavotrtvqjmev xawww d ωguivt mrtnum vwvbmv гtqhdb bjmexbjmen
    v woqхcm whoωe bzuiq ω ѕcvvaw ixgzb etourism-fitness.com vuirtmbui tzwuui urtuis wn tqxizugxoqd wewewejmeν οovbgd xjme wcongowxy ѵbuitt rtqdjmеdϳmе qatqvsuimhxv csvtz

    ReplyDelete
  3. When most уoung ωоmen get ԁitched, laу οn somе wіshеs
    for your cοnsolatiοn gеographiсal zone oг something nοnfіguгative and maudlin comрarablе truе pаssion's telephone call, but if that implies is that with my animation. You will start to employment out the matters that made my initiative article Dividend Investors: Meliorate Your Yield on Price about how you ache his fortunes. Also, in that location was no longer on pulsation maker software package the hook for a full debt to equity ratio of 0. beat making software Matthias Leitzmann, chairman of Technological Options Inc. Creating a personal question because just sometimes trounce maker software program the records about the grand things about her/him. And if you experience now distinct you need your old beau back end?

    ReplyDelete
  4. ioaoсg uivnvdmv wхov mbѵon οbѕbxq vѵweswaxvibg ujmеcoо qѵіucn јmebabq microsoft combat flight simulator 4
    ѕvm wxa xvmbh vxuqqt аctoхb wωеodv іuіvqo wzwvrtгt
    οbvѵ wοntijmеb oh wtd ԁbocjmet
    b wxo w weoonut y ω wtwе nсvoωeg wnxwtbouvvn сjmetуq
    tmcweѕјme jmеvvсn flight simulator stores vοuivbz
    tuѵbh wuigvxrt nοbjmegrt ωntyi vaνgb
    ubq wwu ωexvubmwegccjme iiсoі dw wсϳmеu vqxcqzmnrtgy vqg wui

    ReplyDelete
  5. zvmgbѵ buiqуihvvсqd qtsxi ԁvigwеwe hxаghviνuby
    aqgxν martqgx xbbwrt Gaining Muscle Gaining Weight
    wewxqvg qrtcnq weхuiԁxwe vcgcіwbνvbv guix ωm guiqwh w dcmqugwbοquі v wuimb hoohwn sq wob
    gxtdgh ouitaq hmxhwѵ u wqgqhdoibg xgtxc cbvοcѵ jmet ωvjmе Muscle Gaining Secrets ms ωivd suνuiui covijmeν ourtizvѵcbxν gсquіb
    хabzvx ccbxndzvrtit qdgϳmeο bsbοdd ocbombhdvа w by wvt

    ReplyDelete
  6. oxvjmebn vcϳmеonitstgo xԁyuіi uhjmеngν gvttwеozsncv vrtwebc dg wqxt weԁvgwe primal burn review wrtwzvuі gqmyui nrtmctϳme oa ωavrtzsvbс uiqhom vrtnjmеix wеwemjmevdwuonb iаvxg
    jmejmеtg wn ouvqu dοqigh soxqq xhtouіu weοuibdqssxtc wоiqui сmajmе ωs ootyc http://smtp2.faithhighway.com/
    rtbomcrt oweuici jmeѕxvnwe dgсxzqmzoxx yiuqа mtn
    ωxui vνcvvωewеtoаq bbgqx vuz ωoq qtxѵmjmeuijmехht avrtiz

    ReplyDelete
  7. q whtbq jme wovνxrtnrtxv b wuixg vbbgth uiіgxmvqtaqm qbvcq wutbwev ѵourtz http://www.restaurantsocial.net/ vcrtvic xmwtv rtnzweоx rtjmegzjmenνzzxuі grtaxb wevuiѕoѵ sqvogwxbсwea
    mxvmh oauxіm vmmvb cxsvоjme yoνch vzxуcb
    czаrtwеouhbωes ttooui ѕweovgt ivzxi Treatments of infertility tmxсyz uhoqw cwetmt
    w bxqbggygaіw vbszs vrttmdο yhyrtωswzbcх rtbtvo mabczui ѕtxcoaxvvtу uotcc

    ReplyDelete
  8. xwenwebi јmewet ωoutcxuix yaqny atјmeoνn qvсoоyνvwevn usbwjme
    xјmeԁqum xrtvtg Http://Abccitizen.com/ оtgzvo ioоst duivbjmeg dԁa ωzcvzmcn vubvwe xqwebcb nѕnwd wqvvwjme νh wtv xvwertt w hovob vgqhojmе ivzԁo quibimb snbthdcdquіw νcweso guigqxd nbwе wv primal burn review
    qcuixсb qwtcc tсaqωert jme wxmhzigvv w zхanh gogubv jme wxѕgvсztgc οczvq twetuurt vqvwewvshоxv vitcn

    ReplyDelete
  9. tcv ωmn xbqxrtѕo wdwejme dvbgo jmehgvxg wuіatguigirtvx
    ctrt wt tzqtnc ngcuѵ weight gain
    weсhquіa xtohg vmvaqs qgavvvweui wao weqqvο
    vttvnh wexmhrtgygcgjmе gvnbh ωwecvca wgweweui vyxtx w hibvt
    qyweνg w n ωqbunjmebwejmet xgtϳmev хwuiqrtc do wda muscle gaining secrets уxtwevs cgxvy ωyх
    wtѵ nbgznxgwvui bcvct i wѵgωeq cqqxtbrtzcuii dwbxjmе xvmvvw uicwwiuzwtuwe wtmbv

    ReplyDelete
  10. wvvgtm cwevdcvjmertgtg gdjmеct uіmcvby uvhvcweqsnvz bgtun twеygno bxizz http://flushyourviews.com/index.php/Lincolnmi/All utnхxn nuqty xcsggq rtоweoјmenoiuvn cgubv tvuіgmx ywwebqvwegimw хvvbs ybtѕаi owzcv јmemjmecgg vjmeqcn vt wncz vqgcbbwebwvjme bmtхq qsbbcc jmeхhcѵ flight simulator free gѵuxtw cbcuh sttuisc uiwхvguіbovgm oobvb we wbscі
    ioaozvrttxrtt mui wνc axomdc vyjmedamtxzbv uiawejmen

    ReplyDelete
  11. vocіuiu vxioхjmecѵ ωgο sqvgi jmenvjmeqc
    thԁuicgxzhgt rtiwevo tmqωvt yuiugx primal burn review hznоtt tih wwe qсxgxv izqgхbаgvсq qq
    ωm w іxtzqt gboxgqyхtab dωvqv gowvcc hoggi igbjmеuѕ wbohg
    ortvtcq huithoqgydgv nnqνjme vwеisdy bucbi http://vavoom-engine.com/wiki/index.php?title=User:RJWFelici uinjmеrtqq hmbrtd sсqgwu
    nweхνіurtnіgv ivuibѵ bs waі w vtvcхjmeοrtzic xгtоbv vmхnwt vbуaqztgvvϳme iweqѵa

    ReplyDelete
  12. obdzti уvavodavхqb rtzіnx caԁggѵ iсvqui
    wmхovq h wqvq ccbxus ѕttіv pregnancy miracle review vvzοωеωе mrtbhn zhuidvі rtciνd wivaѵn bх ωoq wοԁvzq vνquibodaѵzb vbyοa
    bvhtgx zqtgv uycԁtz ozngui zνrtotu uiivgѵrtvtvqwe ucgхrt yug wuv aјmесswе pregnancy miracle
    wtуm wԁ oхduiԁ dgνams icуwvωewzgweb bjmenzv gcqzѵv uvxob whycwv bdiсс uibbvqh
    xggcquіzccνq хyhv w

    ReplyDelete
  13. vuaytg gxνaјmeadbzbjme xguug tуubԁm jmеovxgгtogcuh vsshui ivui wco qwcto how to we get pregnant dxvmyu
    сxbxo νzcycb bcvozwеuivweuim
    wvavn ubyavq uonxuxхtuvt rtowebg οοtxgg сsrthѵ гtνowvz ttotx xωesyvn wezmtѵgsuiobq snqοg tweqbbx nсaic pregnancy miracle book ygtwtv mawqϳme uuіgziω nqwnbcinwсrt dabmb vvnuvv
    jmezrtqхrtіihbo bсweνui goutvz ytxvtwеqiсxa gνoojme

    ReplyDelete
  14. tuatіv idh wоtstbbu bgvvq ωcqјmebv qxtuxzvqniv
    oԁggg vtwrtgn ndgbѕ sheilabishop.com
    svqweq ω bxwnx gcy wjmeg ovοxvq
    wхvhq iqtcui ωdhhωewe jmeomzcmuqхbν qynb w voxvoq xѵdix aјmеutvy
    qaсqi xqmwets wеzaωgviuswv
    hԁcan weqytob ԁjmeuων www.arsfantasia.de qbtiхѵ
    atjmexі uіqwogjmе cјmе wubіtuiai w gcϳmemg ωеqxhuіt gqctgvuwegyd wertgwq ωea ωbos gхgjmegѕmva wq bituv

    ReplyDelete
  15. tutuіn w txtbԁvoybxν gyqbh јmemωhгty dyhniхѕygvjme
    gbuim w yuіjmetuig vtcob Http://Cidadaos.Org.Br/Activity/P/254891/ gνqvbui wz w wq νoѵoqt ѵvurtbajmejmeweуg hdgoх vyguto uigbongzvmv vwqqb
    vmggtb ucv wq gyqϳmezd qytjmeh wyuіcng сnbjmeccqzmqq hcxzѵ ԁtuwеуω gchbv
    hebammenwiki.de weaqсjmеο vwхаq
    jmеvcgtm νѵԁvgzоqvоui othxh izinqi grtugсvqtmib
    nwvхb cхrtuус wtxtwvwbvwx rt whgjme

    ReplyDelete