-->
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.  [ 4 posts ] 
Author Message
 Post subject: Stored procedure resulset mapping
PostPosted: Tue Jun 28, 2005 5:20 am 
Newbie

Joined: Mon Jun 27, 2005 11:57 am
Posts: 3
Hi,

I have to call some stored procedures on a 4D database and to map the resultsets.
Here is an example :
XX_StoredProc_GetPContac take 3 parameters (int,int,String) and return a resulset that I just want to map to Document entities.

Hibernate version:3.0.5

Mapping documents:
<?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 package="com.orkis.ajaris.hibernate">

<class name="Document">
<id name="ref" type="string" />
<property name="titre" type="string" not-null="true"/>
<property name="blob" type="blob" not-null="true"/>

<loader query-ref="pContact"/>
</class>

<sql-query name="pContact" callable="true">
<return class="Document">
<return-property name="ref" column="spTabDocRef"/>
<return-property name="titre" column="spTabDocTitre"/>
<return-property name="blob" column="spTabPreviewBlob"/>
</return>
{ ? = call XX_StoredProc_GetPContact(?, ?, ?) }
</sql-query>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Transaction tx = session.beginTransaction();

List docs =session.getNamedQuery("pContact")
.setInteger(1,1)
.setInteger(2,12)
.setString(3,"neige")
.list();

Iterator it = docs.iterator();
while(it.hasNext()){
Document d =(Document)it.next();
System.out.println(d.getTitre()+" blob size: "+ d.getBlob().length()+ " ref : "+d.getRef());
}

tx.commit();


Full stack trace of any exception that occurs:
java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at java.util.ArrayList.toArray(Unknown Source)
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 HibernateTest.main(HibernateTest.java:25)


HibernateTest.main(HibernateTest.java:25) -> list()

Name and version of the database you are using:
4D


I don't know if my exception is due to a bad mapping or if it comes from my JDBC driver.
I have customized the 4D JDBC driver, to support resulset retrieving from stored procedures.
I've tried in JDBC and it works well, but some classes or method are not implemented so I thought that the error could come from the driver, but the exception doesn't really help me to find the problem.

For information i use the GenericDialect.

I hope that someone will be able to help me finding the problem.

Thanks for your help


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 6:44 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
the first ? is not a real parameter.

try setting parameter 0, 1 and 2 instead of 1, 2 and 3

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 8:35 am 
Newbie

Joined: Mon Jun 27, 2005 11:57 am
Posts: 3
Thanks for answering so quickly max,

you are right, i've replaced 1,2,3 by 0,1,2 like that
Code:
Query q  =session.getNamedQuery("pContact")
      .setInteger(0,1)
      .setInteger(1,12)
      .setString(2,"neige");


and then i replaced in the mapping file, the stored procedure call
Code:
? = call XX_StoredProc_GetPContact(?, ?, ?)

by :
Code:
call XX_StoredProc_GetPContact(?, ?, ?)


because the JDBC driver didn't recognize the first form.

I had to write a more specific Dialect, because i had the following error with the generic one :
Code:
java.lang.UnsupportedOperationException: org.hibernate.dialect.GenericDialect does not support resultsets via stored procedures.
   at org.hibernate.dialect.Dialect.registerResultSetOutParameter(Dialect.java:791)
   at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1175)
   at org.hibernate.loader.Loader.doQuery(Loader.java:390)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
   at org.hibernate.loader.Loader.doList(Loader.java:1593)
   at org.hibernate.loader.Loader.list(Loader.java:1577)
   at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:112)
   at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1414)
   at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:153)
   at HibernateTest.main(HibernateTest.java:26)


I have just implemented the following methods in my dialect, and extend GenericDialect class:
Code:
package com.orkis.ajaris.hibernate.dialect;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.hibernate.dialect.GenericDialect;

public class J4DDialect extends GenericDialect {

   public J4DDialect() {
      super();
   }

   public int registerResultSetOutParameter(CallableStatement statement, int col) throws SQLException {
      statement.registerOutParameter(col, java.sql.Types.OTHER,"myResultSet");
      return col++;
   }

   public ResultSet getResultSet(CallableStatement statement) throws SQLException {
      statement.execute();
      ResultSet rs = (ResultSet) statement.getObject(1);         
      return rs;
   }
}



I have verified on the 4d server, the stored procedure is called, the 3 parameters are ok, and the procedure make its "work" and return the good result.
But the problem that I have now is that the list() method returns a void list.
I think that the problem must come from the mapping, because i'm sure that myresultset is not empty. I have launched the test under eclipse in debug mode with breakpoints and i have seen the good values in the resulset.

So does someone has an idea?

thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 28, 2005 9:40 am 
Newbie

Joined: Mon Jun 27, 2005 11:57 am
Posts: 3
it's ok i've found the problem,

i had made a mistake in my resulset implementation :
a beforefirst() was needed before the first next() call on the resulset, i've corrected that and now the mapping works well.

Thanks

++


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