Hi All,
I am trying to delete object using Hibernate's delete method but getting StaleStateException exception.My requirement is to delete an object based on the unique key so for this i am doing something like.
Code:
String queryString="from Destination destination "+
"where destination.destinationID = :destinationid";
tx.begin();
Query query= session.createQuery(queryString).setParameter("destinationid", destinationId, Hibernate.STRING);
@SuppressWarnings("unchecked")
List<Destination> list=query.list();
if(!list.isEmpty()){
Destination ds=list.get(0);
session.evict(ds);
session.delete(ds);
}
tx.commit();
since i know there will be only one element in the list if its not empty so only fetching the first element from the list (Not sure if this is a good approach)
but when i am running this method i can see the following sql log in my console
Code:
Hibernate:
/* delete collection AirTransport.timeTable */ delete
from
tr.TRANSPORTTIME
where
TRANSPORTID=?
Hibernate:
/* delete collection AirTransport.timeTable */ delete
from
tr.TRANSPORTTIME
where
TRANSPORTID=?
Hibernate:
/* delete collection AirTransport.timeTable */ delete
from
tr.TRANSPORTTIME
where
TRANSPORTID=?
Hibernate:
/* delete collection RoadTransport.timeTable */ delete
from
tr.TRANSPORTTIME
where
TRANSPORTID=?
Hibernate:
/* delete collection TrainTransport.timeTable */ delete
from
tr.TRANSPORTTIME
where
TRANSPORTID=?
Hibernate:
/* delete AirTransport */ delete
from
tr.TRANSPORT
where
UUID=?
Hibernate:
/* delete AirTransport */ delete
from
tr.TRANSPORT
where
UUID=?
Hibernate:
/* delete AirTransport */ delete
from
tr.TRANSPORT
where
UUID=?
Hibernate:
/* delete RoadTransport */ delete
from
tr.TRANSPORT
where
UUID=?
and than its throwing
Code:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
one thing which making me suspicious is that in my Transport table i have three entries only one for AirTransport one for Train and last one for Road and in the TransPortTime table i have three entries one for each Transport type and
i am using TransportTime as composite-element in side all three transport classes
Code:
<set name="timeTable" table="TRANSPORTTIME" lazy="true">
<key column="TRANSPORTID"/>
<composite-element
class="TransportTime">
<property name="deperatureTime" type="java.util.Date">
<column name="DEPERATURETIME" />
</property>
<property name="arrivalTime" type="java.util.Date">
<column name="ARRIVALTIME" />
</property>
<other properties>
</composite-element>
</set>
i am still unable to figure out whats going wrong so though of asking in the community
[Update1]
Just to add some extra information: i have the below mapping in my destination class for the three respective trabsport classes
Code:
<set name="airTransport" table="AIRTRANSPORT" inverse="true" lazy="true" cascade="save-update, delete">
<key>
<column name="DESTINATIONID" />
</key>
<one-to-many class="AirTransport" />
</set>
<set name="roadTransport" table="ROADTRANSPORT" inverse="true" lazy="true" cascade="save-update, delete">
<key>
<column name="DESTINATIONID" />
</key>
<one-to-many class="RoadTransport" />
</set>
<set name="trainTransport" table="TRAINTRANSPORT" inverse="true" lazy="true" cascade="save-update, delete">
<key>
<column name="DESTINATIONID" />
</key>
<one-to-many class="TrainTransport" />
</set>
and in my RoadTransport/Train/Air Transport have following mapping
Code:
<class name="AirTransport" table="TRANSPORT">
<id name="uuid" type="java.lang.String">
<column name="UUID" />
<generator class="uuid"/>
</id>
<many-to-one name="destination" class="Destination" fetch="join">
<column name="DESTINATIONID" />
</many-to-one>
<property mappings>
<set name="timeTable" table="TRANSPORTTIME" lazy="true">
<key column="TRANSPORTID"/>
<composite-element
class="TransportTime">
<property mappings>
</composite-element>
</set>
</class>
Other two mapping files are also identical so when i removed mapping association of these three classes from parent class my delete functionality started working perfectly.
So this means i am doing fundamenally wrong in mapping the TimeTable class as component inside Transport classes (Air/Train/Road Transport class)
any suggestion in this regard will be much helpfull
thanks in advance