-->
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.  [ 6 posts ] 
Author Message
 Post subject: problem with stored procedure in hibernate
PostPosted: Wed Feb 25, 2009 2:59 pm 
Newbie

Joined: Wed Oct 15, 2008 12:57 pm
Posts: 11
Hi there,
I am trying to execute the stored procedure in hibernate. I am using spring,hibernate,and sql server 2005.
My mapping is like this
Code:
<sql-query name="TestProc" callable="true" read-only="true">
              {call RunTestProc(?,?,?,?,?,?,?)} 
         
   </sql-query>

and in dao, I can call either
Code:
List<ReturnObjectFromSP> list = this.getHibernateTemplate().findByNamedQuery("TestProc", new Object[]{6,3,9580,2,2,0.1,0});

or
Code:
List<ReturnObjectFromSP> list = this.getSession().getNamedQuery("TestProc")
         .setParameter(0, 6)
         .setParameter(1, 3)
         .setParameter(2, 9580)
         .setParameter(3, 2)
         .setParameter(4, 2)
         .setParameter(5, 0.1)
         .setParameter(6, 0).list();

The problem is it returns list of Object, instead of my ReturnObjectFromSP. So I modify the map to specify the return class,return-property. But it throws exception "Caused by: org.hibernate.HibernateException: Errors in named queries: TestProc".Is it because ReturnObjectFromSP is not mapped to any table? But the result from my stored procedure is combination of different tables.
So I have another try by removing the return class,return-property in the map, and add
Code:
List<ReturnObjectFromSP> list = this.getSession().getNamedQuery("TestProc")
         .setParameter(0, 6)
         .setParameter(1, 3)
         .setParameter(2, 9580)
         .setParameter(3, 2)
         .setParameter(4, 2)
         .setParameter(5, 0.1)
         .setParameter(6, 0).
.setResultTransformer(Transformers.aliasToBean(ReturnObjectFromSP.class)).list();

with this way, it throws another error like it cannot find the setter for the column into my class.
Can anyone give me an advice?
TIA.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2009 3:56 pm 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
Hi,
I think you are close. We did NOT use a Stored Procedure calls from Hibernate in our project but we were returning Table data (from a temp table that the Stored Procedure created and populated) to a third party Report. So the columns names were the aliases and case sensitive.

Can you post your properties in the Class ReturnObjectFromSP? Also can you show how you are returning the data from the stored procedure?

-latha.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2009 6:27 pm 
Newbie

Joined: Wed Oct 15, 2008 12:57 pm
Posts: 11
Thanks for your reply.
This is my ReturnObjectFromSP
Code:
public class ReturnObjectFromSP {
   
   private String name1;
   private String name2;
   
   public ReturnObjectFromSP()
   {
      name1 = "";
      name2 = "";
   }

   public String getName1() {
      return name1;
   }

   public void setName1(String name1) {
      this.name1 = name1;
   }

   public String getName2() {
      return name2;
   }

   public void setName2(String name2) {
      this.name2 = name2;
   }
}

and my stored procedure looks like that
Code:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description:   <Description,,>
-- =============================================
CREAT PROCEDURE [dbo].[RunTestProc]
   @param1 int
   @param2 int
   @param3 int
   @param4 int
   @param5 int
   @param6 int
   @param7 int
AS
select a.name name1, b.name name2 from table1 a,table2 b where a.id=param1 and b.id=param2


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 26, 2009 12:38 pm 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
It worked fine for me. The only thing i found is that when the data types don't match an error stating IllegalArgumentException for the given property.Worked fine with the String data types thou.

Stored Procedure on SQL Server 2005
Table activity has the column ID of type numeric in the database.
Table ele_fld has the column fld_char as varchar(255)

The Numeric columns had to be Generic with Number datatype and not Long or Integer at java end. Hibernate threw exception in those cases.

drop PROCEDURE [dbo].[RunTestProc]
CREATE PROCEDURE [dbo].[RunTestProc]
AS
select
activity.id element,
meta.fld_char metadata
from ele_fld meta
join activity activity on meta.id=activity.id
where meta.id=1002 and meta.type=1

Class:-
public class TestSP {
private Number element;

public Number getElement() {
return element;
}
public void setElement(Number element) {
this.element = element;
}
private String metadata;

public String getMetadata() {
return metadata;
}
public void setMetadata(String metadata) {
this.metadata = metadata;
}
}

Junit test
public void testStoreProcedure(){
Session session=getSession();
List<TestSP> testSPs=session.createSQLQuery("{call RunTestProc}").setResultTransformer(Transformers.aliasToBean(TestSP.class)).list();
for(TestSP testSP:testSPs){
System.out.println(testSP.getElement()+" "+testSP.getMetadata());

}

}

Hope this helps,
latha.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 26, 2009 3:00 pm 
Newbie

Joined: Wed Oct 15, 2008 12:57 pm
Posts: 11
Thanks latha1119,
it's working fine for me if I have the setter has exact name with the column such as if column is id, I have to have setid(int id); setId(int id) wont work. I dont know why?
Again, thanks for your reply.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 28, 2009 2:12 am 
Regular
Regular

Joined: Mon Jan 05, 2009 6:42 pm
Posts: 99
Location: IL
I guess I found out what the issue is..
I ran into the same issue as you did.. For some reason Hibernate treats differently if the Query is:-
Select table1.column1 mycolumn from table table1

Differently than(the keyword 'AS')
Select table1.column1 AS mycolumn from table table1

Hope this helps,
latha.


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