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.  [ 3 posts ] 
Author Message
 Post subject: Hibernate save method throwing StateStaleException
PostPosted: Sat Feb 05, 2011 10:43 am 
Beginner
Beginner

Joined: Mon Sep 20, 2010 2:35 am
Posts: 20
Hi All,

I am trying to save an object in the database using hibernate but facing a problem while saving it.here are the details of the problem. we are getting an XML file from thrid party system so everytime we get an XML file and convert it in to the object so before saving the object we are doing the following steps

1.

Check if there is any entry in the database against the ID (no PK a unique key)
2.

if there is already object we are deleting the object first and than will import the new object using save method
3.

if Object is not in the Database simple import/save the object

third condition is working fine but when we try to save the object in the database for the case where the object is already there in the database it is able to delete the object from the database but when it trying to save the object hibernate is throwing StaleStateException
Code:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1


here is the code for the logic i described above
Code:
temp_destination=getDestinationByDestinationId(destination.getDestinationID());
            if(temp_destination==null){
                    log.info("saving the new object");
                       makePersistance(destination);


                }
                else{

                    deleteDestinationByDestinationId(destination.getDestinationID());
                    saveDestination(destination);

        }


save and delete methods are standard hibernate's save and delete methods.what i noted a strange thing is when i there is no object for the given id in the system it generated following SQL commnands to insert data
Code:
Hibernate:
    /* insert AirTransport
        */ insert
        into
            TR.TRANSPORT
            (DESTINATIONID, FAIR, SOURCE, SERVICEPROVIDERNAME, SERVICETYPE, TYPE, LASTMODIFIED, CREATIONDATE, UUID)
        values

Hibernate:
    /* insert collection
        row AirTransport.timeTable */ insert
        into
            TR.TRANSPORTTIME
            (TRANSPORTID, DEPERATURETIME, ARRIVALTIME, LASTMODIFIED, CREATIONDATE)

    values
        (?, ?, ?, ?, ?)


while there was a record for the given key in the database after it deleted the record for inserting it exceuting entirely different SQL command
Code:
Hibernate:
    /* AirTransport
        */ insert
        into
            TR.TRANSPORT
            (DESTINATIONID, FAIR, SOURCE, SERVICEPROVIDERNAME, SERVICETYPE, TYPE, LASTMODIFIED, CREATIONDATE, UUID)
        values
            (?, ?, ?, ?, ?, ?, ?, ?, ?)

Hibernate:
    /* update
        TR */ update
            TR.TRANSPORT
        set
            DESTINATIONID=?,
            FAIR=?,
            SOURCE=?,
            SERVICEPROVIDERNAME=?,
            SERVICETYPE=?,
            TYPE=?,
        LASTMODIFIED=?
        where
           UUID=?

my .hbm file has following entry
Code:
<hibernate-mapping>
<class="Airtransport">

            <property name="deperatureTime" type="java.util.Date">
                <column name="DEPERATURETIME" />
            </property>
            <property name="arrivalTime" type="java.util.Date">
                <column name="ARRIVALTIME" />
            </property>
    </composite-element>

    </set>
    </class>

My Question is why its generating a different set of SQL for the same opeartion (Save).I even tried to evict the object after it has been removed from the database but result was not as expected.

Edit
what i observe that in these lines

Code:
temp_destination=getDestinationByDestinationId(destination.getDestinationID());

i am fetching destination based on the destinationId which is a unique key though i am closing the session and all other required steps but still gives problem.so in order to test i just removed the line of code to fetch the destination
i just deleted the destination recoded from the database first and than insert it again and it worked fine so it seems thats there is some issue with this logic to fetch the destination first than based on its value delete and again save.

any help in the regard will be helpfull

Thanks
Umesh
http://www.travellingrants.com


Last edited by umesh.awasthi on Sat Feb 05, 2011 2:22 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Hibernate save method throwing StateStaleException
PostPosted: Sat Feb 05, 2011 12:02 pm 
Newbie

Joined: Sun May 09, 2010 4:48 am
Posts: 11
A quick fix would probably be to flush/close the current session after deleting an object, so the session cache is in a consistent state before reinsert.


Top
 Profile  
 
 Post subject: Re: Hibernate save method throwing StateStaleException
PostPosted: Sat Feb 05, 2011 12:15 pm 
Beginner
Beginner

Joined: Mon Sep 20, 2010 2:35 am
Posts: 20
I am already doing the same closing the session in finally block
something
Code:
finally {
         log.info("Closing the session");
         
         if (session != null && session.isOpen() == true)
            session.close();
      }


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