-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: MySQL5Dialect ..no support resultsets via Stored procedures
PostPosted: Thu Mar 02, 2006 1:01 am 
Beginner
Beginner

Joined: Wed Mar 01, 2006 11:26 pm
Posts: 23
Hi im new to Hibernate so please bear with me.

Im trying retrieve a recordset from a MYSQL stored proceedure and am getting the error telling me that the MySQL5Dialect does not support resultsets via stored proceedures. I have also tried this with the ordinary MySQLDialect and get the same problem.

Does anyone have any idea where Im going wrong?

Cheers

Chris

Hibernate version: 3.1

Mapping documents:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://10.1.1.8/ipdb</property>
<property name="connection.username">root</property>
<property name="connection.password">pctfiler</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup>
<property name="hbm2ddl.auto">create</property>
-->

<!--
<mapping resource="com.pctfiler.hibernate.tests.AccountSources.hbm.xml" />
-->

</session-factory>

</hibernate-configuration>



<hibernate-mapping>

<class name="com.pctfiler.hibernate.tests.AccountSources" table="account_sources" catalog="ipdb">

<id name="accountSourceId" type="string"/>
<property name="accountSource" type="string"/>

</class>

<sql-query name="accountSources" callable="true">
<return alias="acc" class="com.pctfiler.hibernate.tests.AccountSources">
<return-property name="AccountSourceId" column="AccountID" />
<return-property name="AccountSource" column="AccountSource" />
</return>
{ CALL TEST() }
</sql-query>

</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Query query = session.getNamedQuery("accountSources");
List result = query.list();


java.lang.UnsupportedOperationException: org.hibernate.dialect.MySQL5Dialect 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 com.pctfiler.hibernate.tests.TestHibernateSessionFactory.testCurrentSession(TestHibernateSessionFactory.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)



MySQL 5

Debug level Hibernate log excerpt:
INFO - Checking 0 named queries
Hibernate: { CALL TEST() }


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 5:09 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
MySQLDialect does support it; no idea why that message is being printed. That should only happen if the Dialect doesn't override getResultSet, which it does. However, your sql-query is wrong, so maybe fixing that will help. You need a "? = " before your call. Have a look in section 16.3.2, "Using stored procedures for querying", of the ref docs: the "? =" is in there, and it's mandatory.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 5:54 pm 
Beginner
Beginner

Joined: Wed Mar 01, 2006 11:26 pm
Posts: 23
Thanks for the reply,

I have tried placing the ? = several times but I still get the same error. Do you have any idea if there is a way that I can get a stored procedure from MySQL5?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 6:19 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
First thing I'd check would be your hibernate jar and version. Verify that org.hibernate.dialect.MySQLDialect, or whatever dialect you're using, defines getResultSet(CallableStatement). The 3.1.1 source code has it, but the 3.0.5 doesn't. It may be that you have one of the last versions that didn't support MySQL result set queries, in which case all you need to do is upgrade.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 6:37 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
Not sure about this with MYSQL, but

in oracle procedures don't return anything
and functions do - so in mysql I'd suspect that
you need to declare it as a function?

the ? = is definitely needed, as tenwit suggested.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 6:46 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
I just found out the syntax on the net... for returning a result set from a procedure... so perhaps this helps:

create procedure select_emps()
begin

select emp_id, emp_name from emps;

end

refer to:
http://mysql.gilfster.com/page.php?parent_id=4&page_id=4.0.3

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 7:10 pm 
Beginner
Beginner

Joined: Wed Mar 01, 2006 11:26 pm
Posts: 23
Ok thanks for the advice.

jt thanks but im pretty sure that were ok using stored proceedures to return data in MySQL. THis advice may come in useful later tho, thanks.

I may have had a conflicing version of hibernate on my my class path, I removed this and am now getting the follwing error:
Hibernate: { ? = CALL TEST() }
WARN - SQL Error: 1305, SQLState: 42000
ERROR - FUNCTION ipdb.TEST does not exist


The stored procedure does exist.

Anyone got any idea? I seem to be going round in circles here.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 7:50 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
post your TEST db code.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 8:49 pm 
Beginner
Beginner

Joined: Wed Mar 01, 2006 11:26 pm
Posts: 23
OK

CREATE PROCEDURE `TEST`()
BEGIN
SELECT AccountSourceID, AccountSource FROM account_sources;
END

Just to let you know that this works no problem when run against the database and returns the following data:

+-----------------+-----------------+
| AccountSourceID | AccountSource
+-----------------+-----------------+
| GOO | Google
| INT | Internet search
| OTH | Other
| PIP | PIPERs website
| POS | Postcard
| WOM | Word of mouth
+-----------------+-----------------+


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 9:14 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I've found that JDBC can get confused when calling stored procedures. Don't know if this is happening to you, but you could try putting the schema name in the proc call. If your schema is dbo (that's the SQLServer default schema, don't know about MySQL), then you'd want to specify your call as "? = call dbo.TEST".

Incidentally, why have you got backquotes around the proc name where you're defining it? Is that a MySQL thing? That would be wrong in SQLServer...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 9:46 pm 
Beginner
Beginner

Joined: Wed Mar 01, 2006 11:26 pm
Posts: 23
I believe the backquotes are a mySQL thing.

If I remove the '? =' from the '? = CALL TEST()' then hibernate regognises the stored proc. However I then get PropertyAccessException.

If I leave the '? =' in the statement then I get the FUNCTION ipdb.TEST does not exist error.

Heres the exception


org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of com.pctfiler.hibernate.tests.AccountSources.setAccountSource
at org.hibernate.tuple.PojoEntityTuplizer.setPropertyValuesWithOptimizer(PojoEntityTuplizer.java:215)
at org.hibernate.tuple.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:185)
at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3232)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:126)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
at org.hibernate.loader.Loader.doQuery(Loader.java:717)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
at org.hibernate.loader.Loader.doList(Loader.java:2145)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)
at org.hibernate.loader.Loader.list(Loader.java:2024)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:111)
at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1674)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:147)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:164)
at com.pctfiler.hibernate.tests.TestHibernateSessionFactory.testCurrentSession(TestHibernateSessionFactory.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: net.sf.cglib.beans.BulkBeanException: java.lang.String
at com.pctfiler.hibernate.tests.AccountSources$$BulkBeanByCGLIB$$3d39666b.setPropertyValues(<generated>)
at org.hibernate.tuple.PojoEntityTuplizer.setPropertyValuesWithOptimizer(PojoEntityTuplizer.java:212)
... 29 more
Caused by: java.lang.ClassCastException: java.lang.String
... 31 more





Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 10:57 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Then you'll have to redefine your proc as a function. By convention, it should return the number of rows in the returned result set. Something like
Code:
create func test
as
begin
  select * from table
  return @@ROW_COUNT
end
At least, that's how to do it in SQLServer. Of course you'll have to translate that to MySQL's dialect.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 11:10 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
ok - I have spent the last 2hrs trying to work through the correct syntax in mysql for returning a ResultSet from a procedure... turns out:

1) you use a procedure, not a function (you guys already knew this...;)
eg:
Code:
CREATE PROCEDURE testProc(in id int)
BEGIN
SELECT myid, uname FROM person;
END;

2) in your jdbc program you call by demo of example code:
Code:
String sql=new String("{CALL testProc(?)}");

cs=m_Conn.prepareCall(sql);
cs.setInt(1,10);
cs.execute();
rs=cs.getResultSet();

    while(rs.next())
    {
     l_sPersonID=rs.getString(1);
     l_sUsername=rs.getString(2);
     System.out.println(l_sPersonID + "," + l_sUsername);
    }

3) here is the create table SQL:
Code:
create table person (`myid` integer,`uname` varchar(30));
insert into person values(1,'bob');
insert into person values(2,'fred');
insert into person values(3,'mary');
commit;


I have NOT tried it with hibernate -- but I'd be willing to bet that if you setup the above demo... and put your call testProc in the <sql-query...
you'll get it working - please confirm.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 02, 2006 11:26 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You'll need to return something from the stored proc, hibernate requires it; but it given this statement:
k209310 wrote:
If I leave the '? =' in the statement then I get the FUNCTION ipdb.TEST does not exist error.

It sounds like MySQL doesn't allow return values from procs. That's why I suggested funcs. But if you can get the stored proc to return a value as well as a result set, and get MySQL to like it, then that's the way to go.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 03, 2006 1:08 am 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
tenwit - I agree with you about the "returning" something... I couldn't figure out from mysql.com or anywhere on the internet how to do a function to return a result set. The docs say that a mysql function can return any MySQL type, and cursor or resultset was not listed as one of the types.

fyi - the syntax problems I was wrestling with wasn't the actual procedure it was getting it loaded into MYSQL - I had to do a weird delimiter call in the mysql session...
(I think I did a "set names utf8", as well, but can't remember for sure right now)

delimiter |
then issue the create procedure statement
and end it with a | instead of the / ... it was really strange.

Code:
delimiter |
CREATE PROCEDURE testProc(in id int)
BEGIN
SELECT myid, uname FROM person;
END;
|

was the way to load it. Else I got some syntax error.

It would be nice if someone who got this working in MYSQL could chime in at this time...yet, I still think it may work w/o the ?= - I'll test it tomorrow.

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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.