-->
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.  [ 8 posts ] 
Author Message
 Post subject: Can call MYSQL stored procedure/function???
PostPosted: Fri Jun 09, 2006 7:09 am 
Newbie

Joined: Fri Jun 09, 2006 5:52 am
Posts: 4
Dear All,
I had tried to use Hibernate to call the stored function in Oracle 9i and it is worked successfully but now the question is does hibernate can call the mysql stored procedure/function?
if yes, how to make it works?

Really need help in order to proceed my work, thanks in advanced.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 12, 2006 11:03 am 
Newbie

Joined: Tue May 23, 2006 3:41 am
Posts: 6
Hi
I've got Hibernate 3.1.3 and mySQL 5.0.18
writing stored procedures with EMS Manager

SP: name sp_proba(myname)
Code:
BEGIN
select clients_id, name, address from clients where name =myname;

END

hbm.file
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

<class name="ee.wallis.crm.win.beans.AAA" table="AAA">
      <id name="Id" type="long" column="clients_id">
         <generator class="assigned" />
      </id>
      <property name="name" type="string" column="name" />
      <property name="address" type="string" column="address" />
</class>


<sql-query name="sp_deliv" callable="true">
<return  class="ee.wallis.crm.win.beans.AAA">
<return-property name="Id" column = "clients_id"/>
<return-property name="name" column = "name"/>
<return-property name="address" column = "address"/>
  </return>
    {call sp_proba(:myname) }
</sql-query>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
             Query query = session.getNamedQuery("sp_deliv");
              query.setString("myname", String.valueOf("Tehnomet"));
             List<AAA> result = query.list();
               for(AAA client: result){
                  System.out.println("Start: " + client.getName());
                  System.out.println("Start: " + client.getAddress());
             }

Be sure that you got class AAA.
If I use SP like filter - it works. When I try update somethink in SP body like
Code:
BEGIN
update projects a, `deliveries` b, `activities` c set c.status_code = newName
where (c.status_code=oldName) AND (b.deliveries_id = deliveryID)
AND (a.projects_id = b.projects_id) AND (b.deliveries_id = c.deliveries_id);

select clients_id, name, address from clients where name =myname;

END
it ignores update part and execute select part.
In manual was written that
Quote:
The stored procedure/function must return a resultset as the first out-parameter to be able to work with Hibernate

is it mean that no any possibility update via SP and Hibernate?
one more thing.
manual give
Quote:
Recommended call form is standard SQL92: { ? = call functionName(<parameters>) } or { ? = call procedureName(<parameters>}. Native call syntax is not supported.


But if I mapping with "{? = call" - program request function. Is that specific of mySQL?


Top
 Profile  
 
 Post subject: Fail to call MYSQL stored procedure
PostPosted: Tue Jun 13, 2006 11:43 pm 
Newbie

Joined: Fri Jun 09, 2006 5:52 am
Posts: 4
Hi All,
I had followed the sample coding provided by oldkeybo but get the error as below. Anyone got any idea how to resolve the problem.

Quote:
exception

org.apache.jasper.JasperException: could not execute query
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:370)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)


root cause

org.hibernate.exception.GenericJDBCException: could not execute query
org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
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:118)
org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1684)
org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:164)
mypackage.DepartmentDAO.departmentName(DepartmentDAO.java:40)
org.apache.jsp.index_jsp._jspService(index_jsp.java:65)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:362)


Below is the mysql stored procedure that I am trying to call
Quote:
CREATE PROCEDURE `myDepartName`()
BEGIN
SELECT dept_id, dept_name FROM t_department;
END


Below is the setting in the Department.hbm.xml
Quote:
<sql-query name="departmentName_SP" callable="true">
<return alias="dept" class="mypackage.Department">
<return-property name="intDeptId" column="dept_id"/>
<return-property name="strDeptName" column="dept_name"/>
</return>
{ call myDepartName() }
</sql-query>


Below is the function used to call the stored procedure
Quote:
public List departmentName() {
Session session = HibernateUtil.getSession();
List lstDept;

lstDept = session.getNamedQuery("mypackage.Department.departmentName_SP").list();

return lstDept;
}


Please advise, thanks in advanced.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 14, 2006 2:34 am 
Newbie

Joined: Tue May 23, 2006 3:41 am
Posts: 6
The Department class should be described in hmb file body and have ID field. If I drop part with description of AAA class -
Quote:
org.hibernate.MappingException: Unknown entity: ee.wallis.crm.win.beans.AAA
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:514)
at org.hibernate.loader.custom.SQLQueryReturnProcessor.getSQLLoadable(SQLQueryReturnProcessor.java:53)
at org.hibernate.loader.custom.SQLQueryReturnProcessor.processRootReturn(SQLQueryReturnProcessor.java:119)
at org.hibernate.loader.custom.SQLQueryReturnProcessor.processReturn(SQLQueryReturnProcessor.java:98)
at org.hibernate.loader.custom.SQLQueryReturnProcessor.process(SQLQueryReturnProcessor.java:87)
at org.hibernate.loader.custom.SQLCustomQuery.<init>(SQLCustomQuery.java:105)
at org.hibernate.engine.query.NativeSQLQueryPlan.<init>(NativeSQLQueryPlan.java:20)
at org.hibernate.engine.query.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:113)
at org.hibernate.impl.SessionFactoryImpl.checkNamedQueries(SessionFactoryImpl.java:410)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:328)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1176)
at ee.wallis.crm.win.actions.activity.proba3.main(proba3.java:30)
Errors in named queries: sp_deliv


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 15, 2006 5:06 am 
Newbie

Joined: Fri Jun 09, 2006 5:52 am
Posts: 4
Hi,
Below is the complete Department.hbm.xml file that I had
Quote:
<hibernate-mapping>
<class name="mypackage.Department" table="t_department">
<id name="intDeptId" type="int">
<column name="dept_id" not-null="true"/>
<generator class="native"></generator>
</id>
<property
name="strDeptName"
type="java.lang.String"
update="true"
insert="true">
<column name="dept_name"/>
</property>

<sql-query name="departmentName_SP" callable="true">
<return alias="dept" class="mypackage.Department">
<return-property name="intDeptId" column="dept_id"/>
<return-property name="strDeptName" column="dept_name"/>
</return>
{ call myDepartName() }
</sql-query>

</class>
</hibernate-mapping>


As I am very new to the MYSQL , can you show me how you define your stored procedure in MYSQL, I mean the complete stored procedure.?

thanks in advanced.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 16, 2006 3:25 am 
Newbie

Joined: Tue May 23, 2006 3:41 am
Posts: 6
Hi,
I'm not sure that I understand wot u mean under complete SP, but will try to help. Once more I recommend EMS manager. http://sqlmanager.net/products/mysql/manager/. This program gives a-la MS Access query builder and helps writing SP, triggers, functions etc. Developers gives 30 days evaluation period.
so...
SP:
Code:
CREATE PROCEDURE `myDepartName`()
    NOT DETERMINISTIC
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN
SELECT dept_id, dept_name FROM t_department;
END;

Department mapping
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="myown.hibernate3.tests.Department" table="t_department">
      <id name="intDeptId" column="dept_id">
         <generator class="native"/>
      </id>
      <property name="strDeptName" column="dept_name"/>
  </class>
   <sql-query name="departmentName_SP" callable="true">
      <return alias="dept" class="myown.hibernate3.tests.Department">
      <return-property name="intDeptId" column="dept_id"/>
      <return-property name="strDeptName" column="dept_name"/>
      </return>
      { call myDepartName() }
</sql-query>

</hibernate-mapping>


class for action
Code:
package myown.hibernate3.tests;

import java.util.Iterator;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


import myown.hibernate3.tests.Department;


public class DepartmentActivity {
    public static void main(String[] args) {
          Session session = null;
          try {
            // This step will read hibernate.cfg.xml and prepare hibernate for
            // use
             
             SessionFactory sessionFactory = new Configuration().configure()
                .buildSessionFactory();
              session =sessionFactory.openSession();
            for (Iterator it = session.getNamedQuery("departmentName_SP").list().iterator();it.hasNext();) {
               Department dep = (Department)it.next();
                  System.out.println("Department: " + dep.getStrDeptName());
             }
             session.close();
             
          } catch (Exception e) {
            System.out.println(e.getMessage());
          } finally {
          }   
        }
}


result
Code:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: { call myDepartName() }
Department: RogaKopyta
Department: Pentagon Ltd
Department: Gonduras Ltd


if my advices help you, please rate my answer


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 12:55 pm 
Beginner
Beginner

Joined: Thu Jun 29, 2006 12:36 pm
Posts: 20
oldkeybo wrote:
if my advices help you, please rate my answer


I don't know if your post helped the starter of the thread, but it helped me to find the error in my code! Thanks! It's a pitty that I can't rate your post.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 08, 2006 9:48 am 
Beginner
Beginner

Joined: Thu Aug 04, 2005 5:06 am
Posts: 31
Location: Bedford, UK
This was extremely useful for me ... thank you very much!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.