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.
Editwhat 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