|
Subject:
|
stored procedures in hibernate3.0.5
|
|
Posted By:
|
itsrahulrd
|
Post Date:
|
6/15/2005 4:21:02 AM
|
how could I pass parameters to a stored procedure in the hibernate mapping file from Java code.
|
|
Reply By:
|
olupas
|
Reply Date:
|
8/3/2005 12:43:59 AM
|
First of all :Hibernate numbers parameters from zero contrary to JDBC
About your question:
This goes in the mapping file:
<query name="eg.DomesticCat.by.name.and.minimum.weight"><![CDATA[ from eg.DomesticCat as cat where cat.name = ? and cat.weight > ? ] ]></query>
Parameter binding and executing is done programatically:
Query q = sess.getNamedQuery("eg.DomesticCat.by.name.and.minimum.weight"); q.setString(0, name); q.setInt(1, minWeight); List cats = q.list();
|
|
Reply By:
|
belugabob
|
Reply Date:
|
1/13/2006 4:58:18 AM
|
The fact that the parameter are indexed from zero can be effectively ignored by using named parameters, like so...
<query name="eg.DomesticCat.by.name.and.minimum.weight"><![CDATA[ from eg.DomesticCat as cat where cat.name = :name and cat.weight > :weight ] ]></query>
Query q = sess.getNamedQuery("eg.DomesticCat.by.name.and.minimum.weight"); q.setString("name", name); q.setInt("weight", minWeight); List cats = q.list();
I'm sure that you'll agree - this is less error prone, more robust (when the query changes) and much more intuitive to read.
|