-->
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.  [ 10 posts ] 
Author Message
 Post subject: Stored procedure execution problem
PostPosted: Tue Aug 14, 2007 3:47 am 
Beginner
Beginner

Joined: Sat May 12, 2007 2:55 am
Posts: 24
Hi friends,
I am trying to execute a stored procedure - MSSQL server using hibernate and I am getting exception. kindly guide. thanks.

below is my .hbm file for table test(id,username,passsord) and my action class from where i am calling SP and lastly my SP.

once again thanks.

Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:3.1

Mapping document:test.hbm.xml
----------------------------------------------

<hibernate-mapping>
<class name="com.Test" table="test" schema="dbo" catalog="ons5dev_vs2">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="assigned" />
</id>
<property name="username" type="java.lang.String">
<column name="username" length="50" />
</property>
<property name="password" type="java.lang.String">
<column name="password" length="50" />
</property>
<sql-query name="sp_testInsert" callable="true">
{ ? = call sp_testInsert(?,?) }
</sql-query>


</class>
</hibernate-mapping>




LoginAction.java
---------------------------------


package gtl.action;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.List;

import gtl.form.LoginForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.HibernateSessionFactory;

public class LoginAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LoginForm loginForm = (LoginForm) form;
System.out.println("You entered username:"+loginForm.getUsername());
System.out.println("You entered password:"+loginForm.getPassword());
int done=saveBySP(loginForm);
if(done==1)
return mapping.findForward("success");
else
return mapping.findForward("fail");
}

public int saveBySP(LoginForm loginForm) {
Session session=HibernateSessionFactory.getSession();
Transaction tx=session.beginTransaction();
Query query=session.getNamedQuery("sp_testInsert");
query.setParameter(1,loginForm.getUsername());
query.setParameter(2,loginForm.getPassword());
List list=query.list();
tx.commit();
return 1;
}
}




Full stack trace of exception that occurs:
-----------------------------------------------------------------
type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Named query not known: sp_testInsert
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

org.hibernate.MappingException: Named query not known: sp_testInsert
org.hibernate.impl.AbstractSessionImpl.getNamedQuery(AbstractSessionImpl.java:70)
org.hibernate.impl.SessionImpl.getNamedQuery(SessionImpl.java:1224)
gtl.action.LoginAction.saveBySP(LoginAction.java:39)
gtl.action.LoginAction.execute(LoginAction.java:29)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)



Name and version of the database you are using:MS SQL Server 9.0
my procedure:


-----------------------
CREATE PROCEDURE sp_testInsert
@username char(50), @password char(50)
AS

insert into test
values(@username,@password)


GO


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 14, 2007 3:58 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Because your named query is inside the <class> element you need to fully qualify it:
Query query=session.getNamedQuery("com.Test.sp_testInsert");


Top
 Profile  
 
 Post subject: another Exception !
PostPosted: Tue Aug 14, 2007 4:06 am 
Beginner
Beginner

Joined: Sat May 12, 2007 2:55 am
Posts: 24
thatmikewilliams wrote:
Because your named query is inside the <class> element you need to fully qualify it:
Query query=session.getNamedQuery("com.Test.sp_testInsert");



Hi William,
thanks for a very fast reply, i could solve the previous exception , but getting below one...


type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Remember that ordinal parameters are 1-based!
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based!
org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterDescriptor(ParameterMetadata.java:55)
org.hibernate.engine.query.ParameterMetadata.getOrdinalParameterExpectedType(ParameterMetadata.java:61)
org.hibernate.impl.AbstractQueryImpl.determineType(AbstractQueryImpl.java:382)
org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:362)
gtl.action.LoginAction.saveBySP(LoginAction.java:41)
gtl.action.LoginAction.execute(LoginAction.java:29)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.5.20 logs.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 14, 2007 4:10 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Parameter positions start from 0 not 1.
query.setParameter(/*1*/ 0,loginForm.getUsername());
query.setParameter(/*2*/ 1,loginForm.getPassword());


Top
 Profile  
 
 Post subject: one more exception
PostPosted: Tue Aug 14, 2007 4:15 am 
Beginner
Beginner

Joined: Sat May 12, 2007 2:55 am
Posts: 24
thatmikewilliams wrote:
Parameter positions start from 0 not 1.
query.setParameter(/*1*/ 0,loginForm.getUsername());
query.setParameter(/*2*/ 1,loginForm.getPassword());


did the changes as per your suggestion, but end up with this newer exception:thanks for your time and efforts.

here is my console trace:
You entered username:nirav
You entered password:nirav
Hibernate: { ? = call sp_testInsert(?,?) }


and this is browser error

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: could not execute query
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

org.hibernate.exception.SQLGrammarException: could not execute query
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
org.hibernate.loader.Loader.doList(Loader.java:2148)
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
org.hibernate.loader.Loader.list(Loader.java:2024)
org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:111)
org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1655)
org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:164)
gtl.action.LoginAction.saveBySP(LoginAction.java:42)
gtl.action.LoginAction.execute(LoginAction.java:29)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 14, 2007 4:42 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Not sure about this one. However, your stored proc doesn't return anything so format shouldn't be "? = call xx", just "call xx". Also, you should probably use query.executeUpdate not query.list


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 14, 2007 4:49 am 
Beginner
Beginner

Joined: Sat May 12, 2007 2:55 am
Posts: 24
thatmikewilliams wrote:
Not sure about this one. However, your stored proc doesn't return anything so format shouldn't be "? = call xx", just "call xx". Also, you should probably use query.executeUpdate not query.list


I suppose we are about to solve. changed call to call xx and used executeUpdate now, it says,

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Update queries only supported through HQL
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

java.lang.UnsupportedOperationException: Update queries only supported through HQL
org.hibernate.impl.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:753)
gtl.action.LoginAction.saveBySP(LoginAction.java:43)
gtl.action.LoginAction.execute(LoginAction.java:29)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


:(


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 14, 2007 4:56 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
So hibernate just doesn't support this. The only example I've seen of using a stored proc for doing inserts is by specifying custom sql in the mapping docs with <sql-insert> etc. This is in the hibernate docs.

Do you have a good reason for using sp for inserting data?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 14, 2007 4:59 am 
Beginner
Beginner

Joined: Sat May 12, 2007 2:55 am
Posts: 24
thatmikewilliams wrote:
So hibernate just doesn't support this. The only example I've seen of using a stored proc for doing inserts is by specifying custom sql in the mapping docs with <sql-insert> etc. This is in the hibernate docs.

Do you have a good reason for using sp for inserting data?


yes, it is a part of moving from one architecture to another. we want to use existing SP that inserts data of a .jsp file to 6 relavant tables. SP is already there, i think i will have to use jdbc callablestatements


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 14, 2007 5:05 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Might be worth investigating the custom sql capabilities.
e.g. from the docs (http://www.hibernate.org/hib_docs/refer ... rysql.html)
Code:
<class name="Person">
    <id name="id">
        <generator class="increment"/>
    </id>
    <property name="name" not-null="true"/>
    <sql-insert callable="true">{call createPerson (?, ?)}</sql-insert>
    <sql-delete callable="true">{? = call deletePerson (?)}</sql-delete>
    <sql-update callable="true">{? = call updatePerson (?, ?)}</sql-update>
</class>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

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.