-->
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.  [ 7 posts ] 
Author Message
 Post subject: Calling stored Procedure inHibernate
PostPosted: Thu Apr 27, 2006 10:08 am 
Newbie

Joined: Thu Apr 27, 2006 9:56 am
Posts: 14
Location: Hyderabad
Hi

I am new to Hibernate. While calling stored Procedure I am encountering the following problem. Can anyone help me out.

I am encountering the following exception

java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at java.util.ArrayList.toArray(ArrayList.java:305)
at org.hibernate.util.ArrayHelper.toTypeArray(ArrayHelper.java:75)
at org.hibernate.impl.AbstractQueryImpl.typeArray(AbstractQueryImpl.java:627)
at org.hibernate.impl.AbstractQueryImpl.getQueryParameters(AbstractQueryImpl.java:635)
at org.hibernate.impl.SQLQueryImpl.getQueryParameters(SQLQueryImpl.java:161)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:153)
at TestClient.main(TestClient.java:39)

The part of code in which i am invoking my stored Procedure is as follows:

Query qry = session.getNamedQuery("selectRecs_SP");

qry.setInteger(1,1);
List ls = qry.list().getClass();

I think my problem is with in code "List ls = qry.list().getClass(); "

But I am unable to rectify this.

Here is my mapping file


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="myex.classfiles.Dept" table="dept">
<id name="Id" column="Id" type="java.lang.Integer">
<generator class="increment"/>
</id>
<property name="name" column="name" type="java.lang.String" />

</class>
<sql-query name="selectRecs_SP" callable="true">
<return alias="dep" class="myex.classfiles.Dept">
<return-property name="name" column="NAME"/>

</return>
{ ? = call sp_get_dept(?) }
</sql-query>


</hibernate-mapping>


If anyone of you have got any solution with these stored Procedures Please help me out.

Thanks

_________________
Rams


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 12:00 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
There are two errors. The one you pointed out is not the one you're currently seeing, but you'll see it as soon as you fix the real error.
Code:
List ls = qry.list().getClass();
Remvoe the ".getClass()" from this. This returns List.class, and that isn't a List, so you'll get a ClassCastException when that executes.

The bug you're currently seeing is due to your definition of the returned result set. You must define all properties using return-property elements. See section 16.3.2, "Using stored procedures for querying", of the ref docs for examples.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 1:30 am 
Newbie

Joined: Thu Apr 27, 2006 9:56 am
Posts: 14
Location: Hyderabad
Thanks for your reply.

List ls = qry.list().getClass() is not my intention. By mistake I it typed here.

I was following the same document you mentioned.

I am unable to trace down which properties i am missing out in my mapping file.

Can you please help me out.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 1:44 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Here's the code from the ref docs:
Code:
<sql-query name="selectAllEmployees_SP" callable="true">
    <return alias="emp" class="Employment">
        <return-property name="employee" column="EMPLOYEE"/>
        <return-property name="employer" column="EMPLOYER"/>
        <return-property name="startDate" column="STARTDATE"/>
        <return-property name="endDate" column="ENDDATE"/>
        <return-property name="regionCode" column="REGIONCODE"/>
        <return-property name="id" column="EID"/>
        <return-property name="salary">
            <return-column name="VALUE"/>
            <return-column name="CURRENCY"/>
        </return-property>
    </return>
    { ? = call selectAllEmployments() }
</sql-query>
As you can see, every single property in Employment is listed as a return-property. So unless your Dept class has only one property, "name", your mapping is wrong.

If you actually want to return only a list of names, then you should not be using <return>, you should be using <return-scalar>. That's described in seciton 16.3.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 1:55 am 
Newbie

Joined: Thu Apr 27, 2006 9:56 am
Posts: 14
Location: Hyderabad
I tried the way you have mentioned ie listing out all the properties.

But still I am facing the same problem when i try to retrieve
List lst = qry.list();

Here is the exception:

java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at java.util.ArrayList.toArray(ArrayList.java:305)
at org.hibernate.util.ArrayHelper.toTypeArray(ArrayHelper.java:75)
at org.hibernate.impl.AbstractQueryImpl.typeArray(AbstractQueryImpl.java:627)
at org.hibernate.impl.AbstractQueryImpl.getQueryParameters(AbstractQueryImpl.java:635)
at org.hibernate.impl.SQLQueryImpl.getQueryParameters(SQLQueryImpl.java:161)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:153)
at TestClient.main(TestClient.java:40)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 2:17 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
That exception is caused by trying to copy something that doesn't extend org.hibernate.type.Type into the parameter list. Unfortunately hibernate doesn't use generics yet, so you're seeing the error at the end of the process, instead of where the error is really happening.

The thing is, you're only calling qry.setInteger(1, 1). The hibernate code in there is very clear, and it's correct. It definitely creates an IntegerType item in the list, and that correctly extends Type. I'm slightly worried by the fact that the parameter at position 0 is defaults to an instance of MarkerObject, which does not extend Type, but I use identical code and it works for me.

I guess you could try "qry.setInteger(0, 0);", to get rid of that marker object, but that smacks of hackery. Still, if it works, it works, so give it a go.

You're not calling Query.setParameters() or Query.setParameterList(), are you? Those have more potential for mistakes, when people accidentally but Integers instead of IntegerTypes in there.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 28, 2006 2:33 am 
Newbie

Joined: Thu Apr 27, 2006 9:56 am
Posts: 14
Location: Hyderabad
That solved my Problem.

Thanks a lot:)


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