I have these two tables (primary keys are in bold):
DOCUMENT (
year,
number, docdata)
ROW (
year,
number,
rownum, idx, rowdata)
with these mappings:
Code:
<class name="Document" table="document">
<composite-id name="key" class="Document$Key">
<key-property name="year" type="integer"/>
<key-property name="number" type="integer"/>
</composite-id>
<property name="docdata" type="string" length="10"/>
<list name="rows" inverse="false" cascade="all,delete-orphan">
<key>
<column name="year"/>
<column name="number"/>
</key>
<list-index column="idx"/>
<one-to-many class="Row"/>
</list>
</class>
Code:
<class name="Row" table="row">
<composite-id name="key" class="Row$Key">
<key-property name="year" type="integer"/>
<key-property name="number" type="integer"/>
<key-property name="rownum" type="integer"/>
</composite-id>
<property name="rowdata" type="string" length="10"/>
<many-to-one name="parent" class="Document" insert="false" update="false">
<column name="year"/>
<column name="number"/>
</many-to-one>
</class>
and these Java classes:
Code:
public class Document {
public static class Key implements Serializable {
private Integer year;
private Integer number;
// getters/setters omitted
}
private Key key;
private String docdata;
private List<Row> rows = new ArrayList<Row>();
// getters/setters omitted
public void addRow(Row row) {
// code omitted: in short, it calculate a field rownum for the row and add the row object to the
// rows list (only if the row object does not yet belongs to the rows list)
}
public void deleteRow(int idx) {
rows.remove(index);
}
}
Code:
public class Row {
public static class Key implements Serializable {
private Integer year;
private Integer number;
private Integer rownum;
// getters/setters omitted
}
private Key key;
private Document parent;
private Integer idx;
private String rowdata;
// getters/setters omitted except the following:
public void setIdx(Integer idx) {
this.idx = idx;
}
public Integer getIdx() {
return idx;
}
}
Put it short, all seems working well for creating new document (with rows) and saving it (saveOrUpdate); same for loading an existing document, adding some rows and resaving it.
But if I try to delete a row from a document having 4 rows, with Document.deleteRow method, in this way:
Code:
Document doc = new Document();
doc.getKey().setYear(2009);
doc.getKey().setNumber(100);
doc = (Document)session.load(Document.class, mm.getKey());
doc.deleteRow(1);
session.saveOrUpdate(doc);
I get an exception that I can resume with this:
Code:
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
Caused by: java.sql.BatchUpdateException: Batch operation 0 update row set year=null, number=null, idx=null where year=2009 and number=100 and year=2009 and number=100 and rownum=4 has been interrupted. Call «getNextException» ... (sorry, exception is translated from Italian)
I searched all documents back and forth but I cannot figure out where is the mistake. This is driving me crazy.
If someone can help me I wil rearrange this example and I will insert it in the Wiki.
Thank you