private void insertSingleBeanIntoDatabase(Table table, DynaBean bean) throws DataSinkException
{
try
{
boolean needTwoStepInsert = false;
ForeignKey selfRefFk = null;
if (!_platform.isIdentityOverrideOn() &&
_tablesWithSelfIdentityReference.contains(table))
{
selfRefFk = table.getSelfReferencingForeignKey();
// in case of a self-reference (fk points to the very row that we're inserting)
// and (at least) one of the pk columns is an identity column, we first need
// to insert the row with the fk columns set to null
Identity pkIdentity = buildIdentityFromPKs(table, bean);
Identity fkIdentity = buildIdentityFromFK(table, selfRefFk, bean);
if (pkIdentity.equals(fkIdentity))
{
if (_tablesWithRequiredSelfReference.contains(table))
{
throw new DataSinkException("Can only insert rows with fk pointing to themselves when all fk columns can be NULL (row pk is " + pkIdentity + ")");
}
else
{
needTwoStepInsert = true;
}
}
}
if (needTwoStepInsert)
{
// we first insert the bean without the fk, then in the second step we update the bean
// with the row with the identity pk values
ArrayList fkValues = new ArrayList();
for (int idx = 0; idx < selfRefFk.getReferenceCount(); idx++)
{
String columnName = selfRefFk.getReference(idx).getLocalColumnName();
fkValues.add(bean.get(columnName));
bean.set(columnName, null);
}
_platform.insert(_connection, _model, bean);
for (int idx = 0; idx < selfRefFk.getReferenceCount(); idx++)
{
bean.set(selfRefFk.getReference(idx).getLocalColumnName(), fkValues.get(idx));
}
_platform.update(_connection, _model, bean);
}
else
{