Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Other Java > Java Databases
|
Java Databases Discussion specific to working with Java Databases. For other Java topics, please see related Java forums. For database discussions not specific to Java, please see the Database category.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 15th, 2005, 04:21 AM
Registered User
 
Join Date: Jun 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default stored procedures in hibernate3.0.5

how could I pass parameters to a stored procedure in the hibernate mapping file from Java code.

 
Old August 3rd, 2005, 12:43 AM
Authorized User
 
Join Date: Aug 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to olupas
Default

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.min imum.weight");
q.setString(0, name);
q.setInt(1, minWeight);
List cats = q.list();

 
Old January 13th, 2006, 05:58 AM
Registered User
 
Join Date: Jan 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.min imum.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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Stored Procedures umeshtheone SQL Server 2000 0 June 19th, 2007 01:08 AM
stored procedures thillaiarasu ASP.NET 2.0 Basics 2 May 3rd, 2007 07:55 AM
Help me to DO Stored Procedures msbsam SQL Server 2000 3 October 23rd, 2006 01:54 PM
Stored Procedures itHighway SQL Server 2000 3 November 23rd, 2005 10:08 AM
Stored Procedures stu9820 ASP.NET 1.x and 2.0 Application Design 2 January 15th, 2005 04:09 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.