-->
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: Stored procedure to update and delete a record
PostPosted: Thu Mar 17, 2005 7:16 pm 
Newbie

Joined: Thu Mar 17, 2005 6:07 pm
Posts: 1
I can call stored procedure to add a record. But failed to delete and update by calling stored procedure.

Table, object, mapping, and stored procedures are created.
call createDog(?,?) works fine.
But call deleteDog(?) and updateDog(?,?) didn't work.

where does the stored procedure return value goes?
do you have example?
If I have 10 attributes but only can update 2 attributes, can I define a stored procedure with three parameters (one for ID)? How hibernate knows which two fields need to update?

Thanks.

Hibernate version:3.0
Mapping documents:
<class name="org.hibernate.examples.quickstart.Dog" table="dog">
<id name="id" type="int" unsaved-value="null" >
<column name="id" sql-type="integer" not-null="true"/>
<generator class="assigned"/>
</id>

<property name="name"/>

<sql-insert callable="true">{call createDog(?, ?)}</sql-insert>
<sql-update callable="true">{? = call updateDog(?, ?)}</sql-update>
<sql-delete callable="true">{? = call deleteDog(?)}</sql-delete>
</class>

Code between sessionFactory.openSession() and session.close():
Transaction tx = null;
try {
tx = session.beginTransaction();
Dog dog = null;
Integer id = new Integer(123);
dog = (Dog)session.load(Dog.class,id);
session.delete(dog);
tx.commit();
System.out.println("delete dog with ID " + dog.getId());
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
}

Full stack trace of any exception that occurs:
Hibernate: select dog0_.id as id0_, dog0_.name as name1_0_ from dog dog0_ where dog0_.id=?
Hibernate: {? = call deleteDog(?)}
(def.AbstractFlushingEventListener 277 ) Could not synchronize database state with session
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [org.hibernate.examples.quickstart.Dog#123]
at org.hibernate.persister.entity.BasicEntityPersister.check(BasicEntityPersister.java:1416)
at org.hibernate.persister.entity.BasicEntityPersister.delete(BasicEntityPersister.java:2042)
at org.hibernate.persister.entity.BasicEntityPersister.delete(BasicEntityPersister.java:2184)
at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:59)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:669)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:293)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at org.hibernate.examples.quickstart.test.DogTest.deleteDog(DogTest.java:102)
at org.hibernate.examples.quickstart.test.DogTest.main(DogTest.java:27)
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [org.hibernate.examples.quickstart.Dog#123]
at org.hibernate.persister.entity.BasicEntityPersister.check(BasicEntityPersister.java:1416)
at org.hibernate.persister.entity.BasicEntityPersister.delete(BasicEntityPersister.java:2042)
at org.hibernate.persister.entity.BasicEntityPersister.delete(BasicEntityPersister.java:2184)
at org.hibernate.action.EntityDeleteAction.execute(EntityDeleteAction.java:59)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:239)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:223)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:669)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:293)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:86)
at org.hibernate.examples.quickstart.test.DogTest.deleteDog(DogTest.java:102)
at org.hibernate.examples.quickstart.test.DogTest.main(DogTest.java:27)

Name and version of the database you are using:
Ingre 3.0


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 18, 2005 2:32 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
not possible for the current constructs since we expect the stored procedure to do the same as the corresponding insert,update sql would do.

the return value (as per doc) is the number of rows updated and that is something hibernate uses to validate the operation.

if you want very specialized sp invocation then you still need to use raw jdbc.

we might consider having sp support for batch update/delete but that is no promise ,)

_________________
Max
Don't forget to rate


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.