Here is our test case:
We have a Subject, whose ID is 70100000000000000000000000000017, that has a collection of 4 Attributes. We are attempting to delete Attribute 11100000000000000000000000000491 which is in indexed position 1 (of a zero based index)
Here is the code from the jUnit test:
Code:
...
String subjectId = "70100000000000000000000000000017";
String attributeId = "11100000000000000000000000000491";
// delete
assertTrue(((HibernateSubjectDAO) getMockDAO())
.deleteAttribute(subjectId, attributeId));
...
Here is the relevant Subject DAO snippet:
Code:
Subject subject = (Subject) session.get(Subject.class, subjectId);
Attribute attribute = (Attribute) session.get(Attribute.class, attributeId);
if (subject.getAttributes().contains(attribute)) {
subject.getAttributes().remove(attribute);
session.update(subject);
session.flush();
success = true;
}
The snippet below is from the debug log:
Code:
DEBUG (BatcherImpl.java:237) - delete from subject_attribute where subject_id=? and sort_order=?
DEBUG (BatcherImpl.java:241) - preparing statement
DEBUG (NullableType.java:46) - binding '70100000000000000000000000000017' to parameter: 1
[color=red]DEBUG (NullableType.java:46) - binding '3' to parameter: 2[/color]
DEBUG (AbstractCollectionPersister.java:575) - done deleting collection rows: 1 deleted
DEBUG (AbstractCollectionPersister.java:710) - Updating rows of collection: com.casebank.spotlight.domain.Subject.attributes#70100000000000000000000000000017
The red highlighted text shows that 3 is being passed for the sort_order (index) parameter. However, in our jUnit test we are in fact attempting to remove sort_order (index) 1 from the collection.
Further up in the debug log, I see that the session is aware of all the Attributes in the Subject's collection:
Code:
DEBUG (Printer.java:75) - listing entities:
DEBUG (Printer.java:82) - com.casebank.spotlight.domain.Subject{attributes=[Attribute#11100000000000000000000000000485, Attribute#11100000000000000000000000000490, Attribute#11100000000000000000000000000500], descriptions=uninitialized, names=[22-00-00 AUTOFLIGHT], children=[Subject#70100000000000000000000000000055, Subject#70100000000000000000000000000077], id=70100000000000000000000000000017}
DEBUG (Printer.java:82) - com.casebank.spotlight.domain.Attribute{parentsIds=[70100000000000000000000000000002, 70100000000000000000000000000017], descriptions=uninitialized, attributeValues=uninitialized, questions=uninitialized, attributeType=AttributeType#10300000000000000000000000000006, names=[SymLogic], matchArgs=uninitialized, observationCost=ObservationCost#22100000000000000000000000000001, children=uninitialized, references=uninitialized, id=11100000000000000000000000000490, observationTime=ObservationTime#22200000000000000000000000000003}
[color=red]DEBUG (Printer.java:82) - com.casebank.spotlight.domain.Attribute{parentsIds=[70100000000000000000000000000017], descriptions=uninitialized, attributeValues=uninitialized, questions=uninitialized, attributeType=AttributeType#10300000000000000000000000000005, names=[SymLogic], matchArgs=uninitialized, observationCost=ObservationCost#22100000000000000000000000000001, children=uninitialized, references=uninitialized, id=11100000000000000000000000000491, observationTime=ObservationTime#22200000000000000000000000000003}[/color]
DEBUG (Printer.java:82) - com.casebank.spotlight.domain.Attribute{parentsIds=[70100000000000000000000000000004, 70100000000000000000000000000017, 70100000000000000000000000000022], descriptions=uninitialized, attributeValues=uninitialized, questions=uninitialized, attributeType=AttributeType#10300000000000000000000000000004, names=[SymLogic], matchArgs=uninitialized, observationCost=ObservationCost#22100000000000000000000000000001, children=uninitialized, references=uninitialized, id=11100000000000000000000000000485, observationTime=ObservationTime#22200000000000000000000000000003}
DEBUG (Printer.java:82) - com.casebank.spotlight.domain.Attribute{parentsIds=[70100000000000000000000000000017], descriptions=uninitialized, attributeValues=uninitialized, questions=uninitialized, attributeType=AttributeType#10300000000000000000000000000005, names=[Pressurization problem], matchArgs=uninitialized, observationCost=ObservationCost#22100000000000000000000000000001, children=uninitialized, references=uninitialized, id=11100000000000000000000000000500, observationTime=ObservationTime#22200000000000000000000000000003}
The red highlighted Attribute is in index position 1 and is the entity we are attempting to delete.
Where we run into the actual exception is when Hibernate tries to update the collection after having deleted the previous Attribute:
Code:
DEBUG (BatcherImpl.java:237) - update subject_attribute set attribute_id=? where subject_id=? and sort_order=?
DEBUG (BatcherImpl.java:241) - preparing statement
DEBUG (NullableType.java:46) - binding '70100000000000000000000000000017' to parameter: 2
DEBUG (NullableType.java:46) - binding '11100000000000000000000000000490' to parameter: 1
DEBUG (NullableType.java:46) - binding '1' to parameter: 3
DEBUG (BatcherImpl.java:203) - done closing: 0 open PreparedStatements, 0 open ResultSets
DEBUG (BatcherImpl.java:261) - closing statement
DEBUG (JDBCExceptionReporter.java:36) - SQL Exception
java.sql.SQLException: Violation of PRIMARY KEY constraint 'PK_group_attribute'. Cannot insert duplicate key in object 'subject_attribute'.
This is obviously also incorrect.
Please deposit 2cents here...