List<DataRow> keys;
try {
keys = keysIterator.dataRows(true);
}
catch (CayenneException ex) {
throw new CayenneRuntimeException("Error reading primary key", Util
.unwindException(ex));
}
if (!(query instanceof InsertBatchQuery)) {
throw new CayenneRuntimeException(
"Generated keys only supported for InsertBatchQuery, instead got "
+ query);
}
BatchQuery batch = (BatchQuery) query;
ObjectId id = batch.getObjectId();
if (id == null || !id.isTemporary()) {
// why would this happen?
return;
}
if (keys.size() != 1) {
throw new CayenneRuntimeException(
"One and only one PK row is expected, instead got " + keys.size());
}
DataRow key = keys.get(0);
// empty key?
if (key.size() == 0) {
throw new CayenneRuntimeException("Empty key generated.");
}
// determine DbAttribute name...
// As of now (01/2005) all tested drivers don't provide decent descriptors of
// identity result sets, so a data row will contain garbage labels. Also most
// DBs only support one autogenerated key per table... So here we will have to
// infer the key name and currently will only support a single column...
if (key.size() > 1) {
throw new CayenneRuntimeException(
"Only a single column autogenerated PK is supported. "
+ "Generated key: "
+ key);
}