use of org.apache.cayenne.dba.PkGenerator in project cayenne by apache.
the class DbGenerator method buildStatements.
/**
* Creates and stores internally a set of statements for database schema
* creation, ignoring configured schema creation preferences. Statements are
* NOT executed in this method.
*/
protected void buildStatements() {
dropTables = new HashMap<>();
createTables = new HashMap<>();
createConstraints = new HashMap<>();
DbAdapter adapter = getAdapter();
for (final DbEntity dbe : this.dbEntitiesInInsertOrder) {
String name = dbe.getName();
// build "DROP TABLE"
dropTables.put(name, adapter.dropTableStatements(dbe));
// build "CREATE TABLE"
createTables.put(name, adapter.createTable(dbe));
// build constraints
createConstraints.put(name, createConstraintsQueries(dbe));
}
PkGenerator pkGenerator = adapter.getPkGenerator();
dropPK = pkGenerator.dropAutoPkStatements(dbEntitiesRequiringAutoPK);
createPK = pkGenerator.createAutoPkStatements(dbEntitiesRequiringAutoPK);
}
use of org.apache.cayenne.dba.PkGenerator in project cayenne by apache.
the class DataContextExtrasIT method testCommitChangesError.
@Test
public void testCommitChangesError() {
DataDomain domain = context.getParentDataDomain();
// setup mockup PK generator that will blow on PK request
// to emulate an exception
JdbcAdapter jdbcAdapter = objectFactory.newInstance(JdbcAdapter.class, JdbcAdapter.class.getName());
PkGenerator newGenerator = new JdbcPkGenerator(jdbcAdapter) {
@Override
public Object generatePk(DataNode node, DbAttribute pk) throws Exception {
throw new CayenneRuntimeException("Intentional");
}
};
PkGenerator oldGenerator = domain.getDataNodes().iterator().next().getAdapter().getPkGenerator();
JdbcAdapter adapter = (JdbcAdapter) domain.getDataNodes().iterator().next().getAdapter();
adapter.setPkGenerator(newGenerator);
try {
Artist newArtist = context.newObject(Artist.class);
newArtist.setArtistName("aaa");
context.commitChanges();
fail("Exception expected but not thrown due to missing PK generation routine.");
} catch (CayenneRuntimeException ex) {
// exception expected
} finally {
adapter.setPkGenerator(oldGenerator);
}
}
use of org.apache.cayenne.dba.PkGenerator in project cayenne by apache.
the class DataDomainInsertBucket method createPermIds.
void createPermIds(DbEntityClassDescriptor descriptor, Collection<Persistent> objects) {
if (objects.isEmpty()) {
return;
}
ObjEntity objEntity = descriptor.getEntity();
DbEntity entity = descriptor.getDbEntity();
DataNode node = parent.getDomain().lookupDataNode(entity.getDataMap());
boolean supportsGeneratedKeys = node.getAdapter().supportsGeneratedKeys();
PkGenerator pkGenerator = node.getAdapter().getPkGenerator();
for (Persistent object : objects) {
ObjectId id = object.getObjectId();
if (id == null || !id.isTemporary()) {
continue;
}
// modify replacement id directly...
Map<String, Object> idMap = id.getReplacementIdMap();
boolean autoPkDone = false;
for (DbAttribute dbAttr : entity.getPrimaryKeys()) {
String dbAttrName = dbAttr.getName();
if (idMap.containsKey(dbAttrName)) {
continue;
}
// handle meaningful PK
ObjAttribute objAttr = objEntity.getAttributeForDbAttribute(dbAttr);
if (objAttr != null) {
Object value = descriptor.getClassDescriptor().getProperty(objAttr.getName()).readPropertyDirectly(object);
if (value != null) {
Class<?> javaClass = objAttr.getJavaClass();
if (javaClass.isPrimitive() && value instanceof Number && ((Number) value).intValue() == 0) {
// primitive 0 has to be treated as NULL, or
// otherwise we can't generate PK for POJO's
} else {
idMap.put(dbAttrName, value);
continue;
}
}
}
// skip db-generated
if (supportsGeneratedKeys && dbAttr.isGenerated()) {
continue;
}
// skip propagated
if (isPropagated(dbAttr)) {
continue;
}
// already in this loop, we must bail out.
if (autoPkDone) {
throw new CayenneRuntimeException("Primary Key autogeneration only works for a single attribute.");
}
// finally, use database generation mechanism
try {
Object pkValue = pkGenerator.generatePk(node, dbAttr);
idMap.put(dbAttrName, pkValue);
autoPkDone = true;
} catch (Exception ex) {
throw new CayenneRuntimeException("Error generating PK: %s", ex, ex.getMessage());
}
}
}
}
Aggregations