Hibernate version:2.1.4
Name and version of the database you are using:
MS SQL
Essential fragments of mapping documents:
Code:
<hibernate-mapping>
<class
name="com.comarch.pc.importd.db.TmpTransaction"
table="LCT_TMP_TRANSACTIONS"
dynamic-update="false"
dynamic-insert="false"
>
<id name="id" column="TTR_ID"
type="int" unsaved-value="0">
<generator class="native">
</generator>
</id>
<set
name="tmpDetails"
lazy="false"
inverse="true"
cascade="all-delete-orphan"
sort="unsorted"
order-by="TTD_ID"
>
<key column="TTD_TTR_ID"/>
<one-to-many
class="com.comarch.pc.importd.db.TmpTrnDetail"/>
</set>
<!-- other properties -->
</class>
</hibernate-mapping>
<hibernate-mapping>
<class
name="com.comarch.pc.importd.db.TmpTrnDetail"
table="LCT_TMP_TRN_DETAILS"
dynamic-update="false"
dynamic-insert="false"
>
<id name="id" column="TTD_ID"
type="int" unsaved-value="0">
<generator class="native"/>
</id>
<many-to-one
name="tmpTransaction"
class="com.comarch.pc.importd.db.TmpTransaction"
cascade="none"
outer-join="auto"
update="true"
insert="true"
access="property"
column="TTD_TTR_ID"
not-null="true"
/>
<!-- other properties -->
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
net.sf.hibernate.Transaction tx = session.beginTransaction();
// Load TmpTransaction with it's TmpDetails
TmpTransaction tmpTxn =
(TmpTransaction)session.get(TmpTransaction.class, new Integer(76097));
System.out.println("Transaction:"+tmpTxn.toString());
Set details = tmpTxn.getTmpDetails();
System.out.println("Details:"+details.size());
// Delete TmpTransaction with it's TmpDetails
session.delete(tmpTxn);
System.out.println("After delete");
tx.commit();
System.out.println("After commit");
// Empty transaction - only for printing state of cache
System.out.println("new transaction");
tx = session.beginTransaction();
System.out.println("Commit");
tx.commit();
Output (<!-- --> - my comments):Code:
[DEBUG]:net.sf.hibernate.transaction.JDBCTransaction: begin
....
<---- loading TmpTransaction and it's TmpDetails ------------------------->
[DEBUG]:net.sf.hibernate.impl.SessionImpl: loading [com.comarch.pc.importd.db.TmpTransaction#76097]
....
[DEBUG]:net.sf.hibernate.impl.SessionImpl: initializing collection [com.comarch.pc.importd.db.TmpTransaction.tmpDetails#76097]
....
[DEBUG]:net.sf.hibernate.impl.SessionImpl: collection fully initialized: [com.comarch.pc.importd.db.TmpTransaction.tmpDetails#76097]
....
Transaction:ID:76097
Details:4
Before delete tmpTxn
<---- delete TmpTransaction ------------------------------------------------->
[DEBUG]:net.sf.hibernate.impl.SessionImpl: deleting a persistent instance
[DEBUG]:net.sf.hibernate.impl.SessionImpl: deleting [com.comarch.pc.importd.db.TmpTransaction#76097]
[DEBUG]:net.sf.hibernate.engine.Cascades: processing cascades for: com.comarch.pc.importd.db.TmpTransaction
[DEBUG]:net.sf.hibernate.engine.Cascades: cascading to collection: com.comarch.pc.importd.db.TmpTransaction.tmpDetails
[DEBUG]:net.sf.hibernate.engine.Cascades: cascading to delete()
[DEBUG]:net.sf.hibernate.impl.SessionImpl: deleting a persistent instance
[DEBUG]:net.sf.hibernate.impl.SessionImpl: deleting [com.comarch.pc.importd.db.TmpTrnDetail#179292]
<---- deleting TmpTrnDetail x4 --------------------------------------->
....
[DEBUG]:net.sf.hibernate.engine.Cascades: done processing cascades for: com.comarch.pc.importd.db.TmpTransaction
[DEBUG]:net.sf.hibernate.engine.Cascades: processing cascades for: com.comarch.pc.importd.db.TmpTransaction
[DEBUG]:net.sf.hibernate.engine.Cascades: done processing cascades for: com.comarch.pc.importd.db.TmpTransaction
After delete
<---- commit -------------------------------------------------------------->
[DEBUG]:net.sf.hibernate.transaction.JDBCTransaction: commit
[DEBUG]:net.sf.hibernate.impl.SessionImpl: flushing session
[DEBUG]:net.sf.hibernate.impl.SessionImpl: Flushing entities and processing referenced collections
[DEBUG]:net.sf.hibernate.impl.SessionImpl: Processing unreferenced collections
[DEBUG]:net.sf.hibernate.impl.SessionImpl: Collection dereferenced: [com.comarch.pc.importd.db.TmpTransaction.tmpDetails#76097]
<----- ^^^^^^^^^^^^^^^^^^^^^^^^ ------->
[DEBUG]:net.sf.hibernate.impl.SessionImpl: Scheduling collection removes/(re)creates/updates
[DEBUG]:net.sf.hibernate.impl.SessionImpl: Flushed: 0 insertions, 0 updates, 5 deletions to 5 objects
[DEBUG]:net.sf.hibernate.impl.SessionImpl: Flushed: 0 (re)creations, 0 updates, 1 removals to 1 collections
[DEBUG]:net.sf.hibernate.impl.Printer: listing entities:
[DEBUG]:net.sf.hibernate.impl.Printer: com.comarch.pc.importd.db.TmpTransaction{..., tmpDetails=[TmpTrnDetail#179292, TmpTrnDetail#179293, TmpTrnDetail#179294, TmpTrnDetail#179295], id=76097}
[DEBUG]:net.sf.hibernate.impl.Printer: com.comarch.pc.importd.db.TmpTrnDetail{...}
<------ x4 ------------------------------------>
[DEBUG]:net.sf.hibernate.impl.SessionImpl: executing flush
[DEBUG]:net.sf.hibernate.persister.EntityPersister: Deleting entity: [com.comarch.pc.importd.db.TmpTrnDetail#179292]
[DEBUG]:net.sf.hibernate.impl.BatcherImpl: about to open: 0 open PreparedStatements, 0 open ResultSets
[DEBUG]:net.sf.hibernate.SQL: delete from LCT_TMP_TRN_DETAILS where TTD_ID=?
[DEBUG]:net.sf.hibernate.impl.BatcherImpl: preparing statement
[DEBUG]:net.sf.hibernate.type.IntegerType: binding '179292' to parameter: 1
[DEBUG]:net.sf.hibernate.impl.BatcherImpl: Adding to batch
<------ x4 --------------------------------------------------------------->
....
[DEBUG]:net.sf.hibernate.impl.BatcherImpl: about to open: 0 open PreparedStatements, 0 open ResultSets
[DEBUG]:net.sf.hibernate.SQL: delete from LCT_TMP_TRANSACTIONS where TTR_ID=?
.....
[DEBUG]:net.sf.hibernate.impl.SessionImpl: transaction completion
After commit
new transaction
<---- empty transaction for printing state of cache ---------------------->
[DEBUG]:net.sf.hibernate.transaction.JDBCTransaction: begin
[DEBUG]:net.sf.hibernate.transaction.JDBCTransaction: current autocommit status:false
Commit
[DEBUG]:net.sf.hibernate.transaction.JDBCTransaction: commit
[DEBUG]:net.sf.hibernate.impl.SessionImpl: flushing session
[DEBUG]:net.sf.hibernate.impl.SessionImpl: Flushing entities and processing referenced collections
[DEBUG]:net.sf.hibernate.impl.SessionImpl: Processing unreferenced collections
[DEBUG]:net.sf.hibernate.impl.SessionImpl: Scheduling collection removes/(re)creates/updates
[DEBUG]:net.sf.hibernate.impl.SessionImpl: Flushed: 0 insertions, 0 updates, 0 deletions to 0 objects
[DEBUG]:net.sf.hibernate.impl.SessionImpl: Flushed: 0 (re)creations, 0 updates, 0 removals to 1 collections
<---- ^^^^^^^^^^^^^^ -->
[DEBUG]:net.sf.hibernate.impl.SessionImpl: executing flush
[DEBUG]:net.sf.hibernate.impl.SessionImpl: post flush
[DEBUG]:net.sf.hibernate.impl.SessionImpl: transaction completion
Description of problem:
There is loaded object TmpTransaction with collection of relative objects TrnDetails. When it is deleting, relative TrnDetails are also deleting. "Cascading" works correctly.
Unwanted effect is, that collection is not evicted from session cache.
When this fragment of code is running in loop, number of cached collections increase drammaticaly. Process get more and more system memory and works slower and slower.
Is there something wrong in my code? Or is it a bug in Hibernate?
Maybe is there another way to remove these collections from session?
"session.evict(details)" doesn't work. I can't completly clear cache (session.clear()) in this case.
Best regards
Lukasz