-->
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.  [ 2 posts ] 
Author Message
 Post subject: Using Stored Procedures to insert data in Mysql DB
PostPosted: Tue Jul 18, 2006 5:12 am 
Beginner
Beginner

Joined: Thu Sep 02, 2004 4:54 am
Posts: 24
Hi!
I need some help because I'm having some trouble using stored procedures on a Mysql DB. I want to change the hibernate generated SQL insert function to a custom SP (stored procedure). I also want the DB to generate the data id with a simple auto-incemented counter.
The problem is that Hibernate does not find the generated id after the SP is called. Here is the code:

Hibernate version:
3.1.3 with MySQL driver 3.1.13

Mapping documents:
<?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="fr.digimind.factory.databean.NewsIndexBean"
table="indexnews"
lazy="false"
>

<id
name="idNI"
column="Id_NI"
type="long"
unsaved-value="any"
>
<generator class="native">
</generator>
</id>

<property
name="id_Site"
type="long"
update="true"
insert="true"
column="Id_Site"
/>


<property
name="urlNI"
type="string"
update="true"
insert="true"
column="Url_NI"
/>

<sql-insert callable="true"> call insert_IndexNews(?,?) </sql-insert>
</class>
</hibernate-mapping>

Full stack trace of any exception that occurs:

DEBUG [org.hibernate.transaction.JDBCTransaction] begin
DEBUG [org.hibernate.jdbc.ConnectionManager] opening JDBC connection
DEBUG [org.hibernate.connection.DriverManagerConnectionProvider] total checked-out connections: 0
DEBUG [org.hibernate.connection.DriverManagerConnectionProvider] using pooled JDBC connection, pool size: 0
DEBUG [org.hibernate.transaction.JDBCTransaction] current autocommit status: false
DEBUG [org.hibernate.jdbc.JDBCContext] after transaction begin
DEBUG [org.hibernate.event.def.DefaultSaveOrUpdateEventListener] saving transient instance
DEBUG [org.hibernate.event.def.AbstractSaveEventListener] saving [fr.digimind.factory.databean.NewsIndexBean#<null>]
DEBUG [org.hibernate.event.def.AbstractSaveEventListener] executing insertions
DEBUG [org.hibernate.persister.entity.AbstractEntityPersister] Inserting entity: fr.digimind.factory.databean.NewsIndexBean (native id)
DEBUG [org.hibernate.jdbc.AbstractBatcher] about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG [org.hibernate.SQL] call insert_IndexNews(?,?)
DEBUG [org.hibernate.jdbc.AbstractBatcher] preparing statement
DEBUG [org.hibernate.persister.entity.AbstractEntityPersister] Dehydrating entity: [fr.digimind.factory.databean.NewsIndexBean#<null>]
DEBUG [org.hibernate.type.LongType] binding '3' to parameter: 1
DEBUG [org.hibernate.type.StringType] binding 'http://www.google.fr' to parameter: 2
DEBUG [org.hibernate.jdbc.AbstractBatcher] about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG [org.hibernate.jdbc.AbstractBatcher] closing statement
DEBUG [org.hibernate.transaction.JDBCTransaction] rollback
DEBUG [org.hibernate.transaction.JDBCTransaction] rolled back JDBC Connection
DEBUG [org.hibernate.jdbc.JDBCContext] after transaction completion
DEBUG [org.hibernate.jdbc.ConnectionManager] aggressively releasing JDBC connection
DEBUG [org.hibernate.jdbc.ConnectionManager] releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
DEBUG [org.hibernate.connection.DriverManagerConnectionProvider] returning connection to pool, pool size: 1
DEBUG [org.hibernate.impl.SessionImpl] after transaction completion
org.hibernate.HibernateException: The database returned no natively generated identity value
at org.hibernate.id.IdentifierGeneratorFactory.getGeneratedIdentity(IdentifierGeneratorFactory.java:33)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1970)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2405)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:37)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:269)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:530)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:518)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:514)
at fr.digimind.evolution.dataaccess.DBMappingImpl.save(DBMappingImpl.java:132)
at fr.digimind.dataaccess.test.DatabaseConnectionTest.main(DatabaseConnectionTest.java:44)
DEBUG [org.hibernate.impl.SessionImpl] disconnecting session
DEBUG [org.hibernate.jdbc.ConnectionManager] connection already null in cleanup : no action


Name and version of the database you are using:
MySQL DB 5.0 using InnoDB engine

[b]the Stored Procedure[b]
CREATE PROCEDURE `insert_IndexNews`( in idSite int, in url varchar(300))
BEGIN

declare _count int;
select count(*) into _count from newsindex i
where i.id_Site = idSite and i.Url_NI = url;

IF (_count = 0)
THEN insert into newsindex (Id_Site, Url_NI) values (idSite, url);
select last_insert_id();
ELSE select 0;
END IF;

END

When I execute this SP on the query browser, the resultset returned contains the last inserted id.

Am I missing somethig?

Thx.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 21, 2006 5:30 pm 
Newbie

Joined: Tue Jun 20, 2006 10:20 pm
Posts: 6
I've run into the same problem. The issue facing your query is that you don't actually have an OUT in the params so your query won't return anything to hibernate.

I haven't found a workaround as adding an OUT to the params list only confuses hibernate and gives me an unexpected number of arugments error. Maybe a function would work as it always has a return.

Does anyone have an example of a stored procedure insert with a database generated ID returned?


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