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