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
|