-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 22 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: How to execute a stored procedure using hibernate
PostPosted: Mon Apr 25, 2005 9:17 am 
Newbie

Joined: Sat Feb 26, 2005 7:34 am
Posts: 14
Location: Bangalore, India
Hibernate version:3.0

I don't know the way exactly that how to use stored procedures
using hibernate. I didn't find any detailed documentation on stored procedures in Hibernate 3.0.
I am using the following code to understand the usage of stored procedures in Hibernate 3.0.

Please anyone help me by providing a simple example!!! Otherwise i will provide my code, please correct it.

Thanks,
Kumar.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 25, 2005 12:31 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
whats wrong with the examples in the hibernate documentation ?

And you didnt seem to include any code in your comment ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 12:28 am 
Newbie

Joined: Sat Feb 26, 2005 7:34 am
Posts: 14
Location: Bangalore, India
ok I am pasting my code, which executes the stored procedure...
-------------------------------------------------------------------------------
Following is the XML mapping document for persisting "Company" object:
----------------------------------------------------------------------------------

<hibernate-mapping>
<class name="hibernate.Company" table="dp_company">
<id column="company_id" name="id" type="long">
<generator class="hilo"/>
</id>
<property column="company_name" length="255" name="companyName" not-null="true" type="string"/>
<sql-delete callable="true">{? = call deleteCompany (?)}</sql-delete>
</class>
</hibernate-mapping>

------------------------------------------------------------------------------------
Following is the stored procedure created in MS-SQL Server 2000:
-----------------------------------------------------------------------------------
CREATE procedure deleteCompany
@ID bigint
as
/*
** Update the company object
*/
begin transaction
delete from dp_company where company_id = @ID
commit
return @@ROWCOUNT
GO
-------------------------------------------------------------------------------------
Following is the code I am executing from command prompt:
-------------------------------------------------------------------------------------
// 1. Build a Company

Company comp = new Company();
comp.setCompanyName(args[0]);
System.out.println(comp);
CompanyPersistenceCrudService comcrud = new CompanyPersistenceCrudService();
comcrud.save(comp);
System.out.println("Company after hibernating:");
System.out.println(comp);


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 1:23 am 
Beginner
Beginner

Joined: Wed Apr 21, 2004 8:33 am
Posts: 27
Its same as you do it using JDBC. Probably you can open the hibernate session, begin the transaction, call the stored procedure,commit the transaction, close the session,if any exceptions rollback.
If you are still having the problem , i will probbaly paste the snippet of the code tommorrow.

Thanks
Yenne


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 3:00 am 
Newbie

Joined: Sat Feb 26, 2005 7:34 am
Posts: 14
Location: Bangalore, India
I need code snippet, I followed all the steps, you can see that from my code which i pasted in my previous message.
Thanks,
Kumar.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 3:31 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Rotte,

I don't know what you are asking for ? You apparently have mapped the thing correctly and I don't see any stacktrace or any errors ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 4:01 am 
Newbie

Joined: Sat Feb 26, 2005 7:34 am
Posts: 14
Location: Bangalore, India
max wrote:
Hi I am not getting any exception or error message. But procedure is not getting executed.

_________________
Thanks,
Kumar.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 4:04 am 
Newbie

Joined: Sat Feb 26, 2005 7:34 am
Posts: 14
Location: Bangalore, India
my question is why my procedure is not getting executed, looks everything correct to me. Why don't you try executing the above code as a standalone application.

_________________
Thanks,
Kumar.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 26, 2005 5:06 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
for one thing - you dont call delete so how do you expect sql-delete to be used in your code ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 28, 2005 3:50 am 
Newbie

Joined: Sat Feb 26, 2005 7:34 am
Posts: 14
Location: Bangalore, India
max wrote:
I tried calling delete() method in my code, but it is not executing stored procedure and if we need to call delete() method then whats the use of stored procedures ? I mean delete() itself is sufficient to delete any instance from database.

Please let me know if I am thinking in some wrong direction.

_________________
Thanks,
Kumar.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 28, 2005 4:00 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Hi Rotte,

I think you got something backwards here ;)

Do you expect hibernate to invoke your stored procedure to delete your Companies without you have told Hibernate to do it ? How should hibernate know that you want to delete something without you calling delete() ?

And if delete() works fine for you then why are you using stored procedures ? Stored procedures is an *option* not an requirement in Hibernate. Its you who decide to use stored procedures instead of the sql generated by hibernate.

And I find it hard to belive that the stored procedure is not called when you call delete() - our unittests states otherwise. you would need to provide a failing test case for this if you still consider it a bug.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 28, 2005 4:54 am 
Newbie

Joined: Sat Feb 26, 2005 7:34 am
Posts: 14
Location: Bangalore, India
max wrote:
Hi Max,

You are absolutely right here. I don't want to frustrate you, however I am still not clear with the following points:

1). Lets assume we have decided to use stored procedures and we need to call delete method. Pefectly fine beacuse hibernate won't call our stored procedure just by writing the following line in hbm.xml mapping file:

<sql-delete callable="true">{? = call deleteCompany (?)}</sql-delete>

But here What I want to know that if we remove the above statement from hbm.xml mapping file , then even just by calling delete(Object o) method I am able to delete the instance from database. Then why one should include that <sql-delete> tag in hbm.xml mapping file.

I am not sure whether you have really understood my doubt or not but here is a issue. Please let me know your inputs.



_________________
Thanks,
Kumar.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 28, 2005 5:06 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Okey I'm soon out of words.

If hibernates default behavior is perfectly valid for you - why are you even adding <sql-delete callable="true">{? = call deleteCompany (?)}</sql-delete> ? <sql-delete> is not *required* its only a hook for you to provide a custom sql string if there is something else you want to be done - like e.g. calling a stored procedure because its a company policy or it does something magically.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 28, 2005 5:18 am 
Newbie

Joined: Sat Feb 26, 2005 7:34 am
Posts: 14
Location: Bangalore, India
max wrote:

Hey Max,

I don't want to do any magic. I am trying calling stored procedures, because I need to execute some legacy stored procedures already existing in our project. Those legacy stored procedures actually contains some complex SQL, and inorder to avoid writing that SQL using HQL or some other way, we want to execute those legacy SPs as it is.

.

_________________
Thanks,
Kumar.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 28, 2005 5:38 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
so I dont understand why you ask *me* why you should use sql-delete, because obviously you should.

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 22 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.