-->
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.  [ 6 posts ] 
Author Message
 Post subject: Problems with delete, session.flush ?!
PostPosted: Sat Jan 07, 2006 1:31 am 
Newbie

Joined: Mon Oct 03, 2005 12:07 pm
Posts: 8
hi

I have some trouble with a simple delete of a row in a Mysql-Database.

The Hibernate-Exception is:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1

or in log files:
Code:
Hibernate: delete from termine_user where UID=?
- Exception executing batch:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
   at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:93)
   at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:79)

....


My Code to delete a row ... :

Code:
     private String deleteGlobalTermin(termine termine){
       String ret = "";
       try {
            System.out.println("deleting termine"+termine.getTERMIN_ID());
          Session session = HibernateUtil.currentSession();
          Transaction tx = session.beginTransaction();       
            session.delete(termine);
            session.flush();
            session.clear();
           
          tx.commit();
          HibernateUtil.closeSession();
          ret = "Success - ";
        } catch( HibernateException ex ) {
           ret = "error1: "+ex;
        } catch ( Exception ex2 ){
           ret = "error2: "+ex2;
        }
        return ret;
    }


and it is really just a single row .. no collection, no set, no bag here is the file termin.hbm.xml:
Code:
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="stz.xmlshop.Hibernate.Beans.termine.termine" table="termine">
      <id name="TERMIN_ID" column="TERMIN_ID">
         <generator class="identity"/>
      </id>
      <property name="description" column="description" type="java.lang.String"/>
      <property name="comment" column="comment" type="java.lang.String"/>
      <property name="open" column="open" type="java.lang.Integer"/>
      <property name="start" column="start" type="java.lang.Long"/>
      <property name="end" column="end" type="java.lang.Long"/>
      <property name="starttime" column="starttime" type="java.lang.Long"/>
      <property name="updatetime" column="updatetime" type="java.lang.Long"/>
      <property name="place" column="place" type="java.lang.String"/>
      <property name="owneruser" column="owneruser" type="java.lang.Integer"/>
      <property name="terminstatus" column="terminstatus" type="java.lang.Integer"/>
   </class>
</hibernate-mapping>



The row IS deleted i just get this Error-Message and i don't know why. First i thought the Object could still be in cache and when hibernate tries to update the objects it realizes that this object isn't there anymore.

I got no clue why, plain SQL-Queries work well, for example:
Code:
Session session = HibernateUtil.currentSession();
          Transaction tx = session.beginTransaction();   
          String hqlDelete = "delete contactgroups where CONTACT_ID = :CONTACT_ID";
           int deletedEntities = session.createQuery( hqlDelete )
                               .setInteger( "CONTACT_ID", CONTACT_ID )
                               .executeUpdate();
            tx.commit();
            HibernateUtil.closeSession();

works fine ...


thx

my System:
OS: debian woody
Database: 4.1.11-Debian_4
MySQL-Driver: mysql-connector-java-3.1.12-bin.jar
Hibernate-Version: version 3.1, Dec 12, 2005

Best,
Sebastian Wagner

_________________
http://www.laszlo-forum.de
http://www.webbase-design.de


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 4:27 am 
Regular
Regular

Joined: Sun Aug 01, 2004 6:49 pm
Posts: 76
Probier mal das Commitment vor dem Flushing und Clearing aus.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 6:11 am 
Newbie

Joined: Mon Oct 03, 2005 12:07 pm
Posts: 8
danke für die antwort aber geht leider auch nicht ....

es heißt immer:
Quote:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1


also sowohl:
Code:
    private String deleteUserTermin(termine_user termine_user){
       String ret = "";
       try {
            System.out.println("deleting termine_user"+termine_user.getUID());
          Session session = HibernateUtil.currentSession();
          Transaction tx = session.beginTransaction();       
            session.delete(termine_user);
          tx.commit();
            session.flush();
            session.clear();          
          HibernateUtil.closeSession();
          
          ret = "Success - ";
        } catch( HibernateException ex ) {
           ret = "error1: "+ex;
        } catch ( Exception ex2 ){
           ret = "error2: "+ex2;
        }
        return ret;
    }


als auch:
Code:
    private String deleteGlobalTermin(termine termine){
       String ret = "";
       try {
            System.out.println("deleting termine"+termine.getTERMIN_ID());
          Session session = HibernateUtil.currentSession();
          Transaction tx = session.beginTransaction();       
            //termine ter = (termine) session.load(termine.class, new Long(TERMIN_ID));
            session.delete(termine);     
            session.flush();
          tx.commit();
           
            //session.clear();             
          HibernateUtil.closeSession();
       
          ret = "Success - ";
        } catch( HibernateException ex ) {
           ret = "error1: "+ex;
        } catch ( Exception ex2 ){
           ret = "error2: "+ex2;
        }
        return ret;
    }


geht beides NICHT ... obwohl zB hier so beschrieben:
http://forum.hibernate.org/viewtopic.php?t=926955

Es scheint so als ob das Objekt immer noch im cache liegt und deshalb beim flush versucht wird es upzudaten. kA warum es nicht beim delete entfernt wird.


NUR das hier funktioniert OHNE Fehlermeldungen/Exceptions:

Code:
    private String deleteGlobalTermin(termine termine){
       String ret = "";
       try {
            System.out.println("deleting termine"+termine.getTERMIN_ID());
          Session session = HibernateUtil.currentSession();
          Transaction tx = session.beginTransaction();       
            String hqlDelete = "delete termine where TERMIN_ID= :TERMIN_ID";
            int updatedEntities = session.createQuery( hqlDelete )
                                .setInteger( "TERMIN_ID", termine.getTERMIN_ID() )
                                .executeUpdate();
            tx.commit();
          HibernateUtil.closeSession();
       
          ret = "Success"+updatedEntities;
        } catch( HibernateException ex ) {
           ret = "error1: "+ex;
        } catch ( Exception ex2 ){
           ret = "error2: "+ex2;
        }
        return ret;
    }


Schade dass man dann doch wieder auf SQL-Statements zurückgreifen muss und nicht nur mit Objekten arbeiten kann.

mfg
sebastian wagner

_________________
http://www.laszlo-forum.de
http://www.webbase-design.de


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 7:19 am 
Pro
Pro

Joined: Mon Jan 24, 2005 5:39 am
Posts: 216
Location: Germany
Das sollte eigentlich funktionieren.
passiert in der Session noch was anderes,
ausser laden und löschen des Objekts ?

Ne andere Sache:
Versuch mal
<property name="jdbc.batch_size">0</property>
in der cfg.xml.
Bei manchen DBs macht das Probleme.

_________________
dont forget to rate !


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 7:56 am 
Newbie

Joined: Mon Oct 03, 2005 12:07 pm
Posts: 8
hi,

nein in der session passiert sonst eigentlich nix, sie wird ja mit:
Code:
HibernateUtil.currentSession();

geöffnet und mit:
Code:
HibernateUtil.closeSession();


wieder geschlossen.

<property name="jdbc.batch_size">0</property>
Hat leider auch nichts gebracht. Ich benütze InnoDB als MySQLDialekt und habe jetzt auch mal auf: MySQLMyISAMDialect umgestellt. Das ändert aber auch leider ncihts am Fehler:
Quote:
- Could not synchronize database state with session
org.hibernate.StaleStateException: Unexpected row count: 0 expected: 1


Funktion:


Code:
    private String deleteGlobalTermin(termine termine){
       String ret = "";
       try {
            System.out.println("deleting termine"+termine.getTERMIN_ID());
          Session session = HibernateUtil.currentSession();
          Transaction tx = session.beginTransaction();       
            //termine ter = (termine) session.load(termine.class, new Long(TERMIN_ID));
            /*String hqlDelete = "delete termine where TERMIN_ID= :TERMIN_ID";
            int updatedEntities = session.createQuery( hqlDelete )
                                .setInteger( "TERMIN_ID", termine.getTERMIN_ID() )
                                .executeUpdate();*/
          session.delete(termine);
            tx.commit();
            session.flush();
          HibernateUtil.closeSession();
       
          ret = "Success";
        } catch( HibernateException ex ) {
           ret = "error1: "+ex;
        } catch ( Exception ex2 ){
           ret = "error2: "+ex2;
        }
        return ret;
    }


danke & mfg
sebastian wagner

_________________
http://www.laszlo-forum.de
http://www.webbase-design.de


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 09, 2006 9:50 am 
Newbie

Joined: Mon Oct 03, 2005 12:07 pm
Posts: 8
ok der fehler lag eindeutig bei mir ... funktioniert jetzt. Ich habe den Fehler selbst provoziert indem ich versucht habe Objekt-IDs zu löschen die es gar nicht mehr gibt.

Ich benutze jetzt:
Code:
Session session = HibernateUtil.currentSession();
             Transaction tx = session.beginTransaction();       
             session.delete(termine);
               tx.commit();
               session.flush();
               session.clear();
             HibernateUtil.closeSession();


Funktioniert einwandfrei .. aber ich überprüfe explixit jedesmal davor ob das Objekt mit dieser ID noch existiert.

Dank an alle Helfer
Mfg
Sebastian Wagner

_________________
http://www.laszlo-forum.de
http://www.webbase-design.de


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