Hello,
After failing to find a solution to this problem in the forums, I am posting it. Hope that somebody can help me. Thanks in advance.
Here is the problem itself:
I have two classes ActivityEntry(AE) and ActivityRecord(AR). The latter is dependent on the prior. The AE class contains a collection of AR classes.
Attempting to deletes some of the AR classes from the collection does not change the state of the database. However removing all ARs from the collection removes them from the DB sucessfully.
The Debug output of Hibernate does nor report any problems and indeed reports the deletion of the AE objects, but data in the DB is unchanged.
Here is sample code:
Session s = this.sf.openSession();
Transaction tx = s.beginTransaction();
ActivityEntry ae = this.findAE(s, 4);
int c = 0;
for (Iterator i = ae.getRecords().iterator(); i.hasNext();)
{
c++ ;
ActivityRecord ar = (ActivityRecord)i.next();
// if (c % 2 == 0)
// {
System.out.println("Removing Activity Record with hashcode: " + ar.hashCode());
i.remove();
// }
}
System.out.println("Size is " + ae.getRecords().size());
s.update(ae);
tx.commit();
s.close();
When the above commented lines are uncommented (Case 1) DB is updated successfully. When commented (Case 2), DB is not changed at all.
Hibertate uses the two cases:
Case 1.
Hibernate: delete from tActivityRecords where cID=?
Debug output:
17:38:24,156 DEBUG BasicCollectionPersister:477 - Deleting collection: [ActivityEntry.records#5]
17:38:24,156 DEBUG BatcherImpl:200 - about to open: 0 open PreparedStatements, 0 open ResultSets
17:38:24,156 DEBUG SQL:226 - delete from tActivityRecords where cID=?
Hibernate: delete from tActivityRecords where cID=?
17:38:24,156 DEBUG BatcherImpl:249 - preparing statement
17:38:24,156 DEBUG IntegerType:46 - binding '5' to parameter: 1
17:38:24,156 DEBUG BasicCollectionPersister:493 - done deleting collection
17:38:24,156 DEBUG BatcherImpl:207 - done closing: 0 open PreparedStatements, 0 open ResultSets
17:38:24,156 DEBUG BatcherImpl:269 - closing statement
17:38:24,156 DEBUG SessionImpl:2824 - post flush
17:38:24,156 DEBUG SessionImpl:585 - transaction completion
17:38:24,156 DEBUG SessionImpl:573 - closing session
17:38:24,156 DEBUG SessionImpl:3336 - disconnecting session
17:38:24,156 DEBUG DriverManagerConnectionProvider:120 - returning connection to pool, pool size: 2
17:38:24,171 DEBUG SessionImpl:585 - transaction completion
Case 2.
Hibernate: delete from tActivityRecords where cID=? and cComment=? and cActivity=? and cProject=? and cWeight=?
Debug output:
17:36:01,968 DEBUG BatcherImpl:200 - about to open: 0 open PreparedStatements, 0 open ResultSets
17:36:01,968 DEBUG SQL:226 - delete from tActivityRecords where cID=? and cComment=? and cActivity=? and cProject=? and cWeight=?
Hibernate: delete from tActivityRecords where cID=? and cComment=? and cActivity=? and cProject=? and cWeight=?
17:36:01,968 DEBUG BatcherImpl:249 - preparing statement
17:36:01,968 DEBUG IntegerType:46 - binding '5' to parameter: 1
17:36:01,968 DEBUG StringType:41 - binding null to parameter: 2
17:36:01,968 DEBUG IntegerType:46 - binding '5' to parameter: 3
17:36:01,968 DEBUG IntegerType:46 - binding '11' to parameter: 4
17:36:01,984 DEBUG IntegerType:46 - binding '100' to parameter: 5
17:36:01,984 DEBUG IntegerType:46 - binding '5' to parameter: 1
17:36:01,984 DEBUG StringType:41 - binding null to parameter: 2
17:36:01,984 DEBUG IntegerType:46 - binding '5' to parameter: 3
17:36:01,984 DEBUG IntegerType:46 - binding '10' to parameter: 4
17:36:01,984 DEBUG IntegerType:46 - binding '100' to parameter: 5
17:36:01,984 DEBUG BasicCollectionPersister:575 - done deleting collection rows: 2 deleted
17:36:01,984 DEBUG BasicCollectionPersister:710 - Updating rows of collection: com.seeburger.bilbo.vo.ActivityEntry.records#5
17:36:01,984 DEBUG BasicCollectionPersister:715 - done updating rows: 0 updated
17:36:01,984 DEBUG BasicCollectionPersister:592 - Inserting rows of collection: [com.seeburger.bilbo.vo.ActivityEntry.records#5]
17:36:01,984 DEBUG BasicCollectionPersister:614 - done inserting rows: 0 inserted
17:36:01,984 DEBUG BatcherImpl:207 - done closing: 0 open PreparedStatements, 0 open ResultSets
17:36:01,984 DEBUG BatcherImpl:269 - closing statement
17:36:01,984 DEBUG SessionImpl:2824 - post flush
ActivityRecord.hashCode(): Returning hashcode - 3716
ActivityRecord.hashCode(): Returning hashcode - 3715
17:36:02,000 DEBUG SessionImpl:585 - transaction completion
17:36:02,000 DEBUG SessionImpl:573 - closing session
17:36:02,000 DEBUG SessionImpl:3336 - disconnecting session
17:36:02,000 DEBUG DriverManagerConnectionProvider:120 - returning connection to pool, pool size: 2
17:36:02,000 DEBUG SessionImpl:585 - transaction completion
The Hibernate mappings are:
Mapping documents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping
>
<class
name="com.seeburger.bilbo.vo.ActivityEntry"
table="tActivities"
>
<id
name="id"
column="cID"
type="int"
unsaved-value="null"
>
<generator class="increment">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-ActivityEntry.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<set
name="records"
table="tActivityRecords"
lazy="false"
cascade="all"
sort="unsorted"
>
<key
column="cID"
>
</key>
<composite-element
class="ActivityRecord"
>
<property
name="comment"
type="java.lang.String"
column="cComment"
/>
<many-to-one
name="activity"
class="ActivityEntry"
cascade="none"
outer-join="auto"
column="cActivity"
/>
<many-to-one
name="project"
class="Project"
cascade="none"
outer-join="auto"
column="cProject"
/>
<property
name="weight"
type="int"
column="cWeight"
/>
</composite-element>
</set>
<property
name="reportDate"
type="java.util.Date"
column="cReportDate"
/>
<property
name="targetDate"
type="java.util.Date"
column="cTargetDate"
/>
<property
name="type"
type="java.lang.String"
column="cType"
/>
<many-to-one
name="user"
class="User"
cascade="none"
outer-join="auto"
column="cUser"
/>
<property
name="remark"
type="java.lang.String"
column="cRemark"
/>
<property
name="breaks"
type="java.util.Date"
column="cBreaks"
/>
<property
name="endTime"
type="java.util.Date"
column="cEndTime"
/>
<property
name="startTime"
type="java.util.Date"
column="cStartTime"
/>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping
>
<class
name="ActivityRecord"
table="tActivityRecords"
>
<id
name="id"
column="cID"
type="int"
unsaved-value="null"
>
<generator class="increment">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-ActivityRecord.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>
<property
name="comment"
type="java.lang.String"
column="cComment"
/>
<many-to-one
name="activity"
class="ActivityEntry"
cascade="none"
outer-join="auto"
column="cActivity"
/>
<many-to-one
name="project"
class="Project"
cascade="none"
outer-join="auto"
column="cProject"
/>
<property
name="weight"
type="int"
column="cWeight"
/>
<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-ActivityRecord.xml
containing the additional properties and place it in your merge dir.
-->
</class>
</hibernate-mapping>
I am using Hibernate version 2.1 and MS SQL 2000.
Any help is appreciated.
|