Search in sources :

Example 1 with PkGenerator

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);
}
Also used : DbAdapter(org.apache.cayenne.dba.DbAdapter) DbEntity(org.apache.cayenne.map.DbEntity) PkGenerator(org.apache.cayenne.dba.PkGenerator)

Example 2 with PkGenerator

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);
    }
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) JdbcAdapter(org.apache.cayenne.dba.JdbcAdapter) DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) JdbcPkGenerator(org.apache.cayenne.dba.JdbcPkGenerator) PkGenerator(org.apache.cayenne.dba.PkGenerator) JdbcPkGenerator(org.apache.cayenne.dba.JdbcPkGenerator) Test(org.junit.Test)

Example 3 with PkGenerator

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());
            }
        }
    }
}
Also used : ObjAttribute(org.apache.cayenne.map.ObjAttribute) ObjectId(org.apache.cayenne.ObjectId) DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent) PkGenerator(org.apache.cayenne.dba.PkGenerator) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity)

Aggregations

PkGenerator (org.apache.cayenne.dba.PkGenerator)3 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)2 DbAttribute (org.apache.cayenne.map.DbAttribute)2 DbEntity (org.apache.cayenne.map.DbEntity)2 ObjectId (org.apache.cayenne.ObjectId)1 Persistent (org.apache.cayenne.Persistent)1 DbAdapter (org.apache.cayenne.dba.DbAdapter)1 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)1 JdbcPkGenerator (org.apache.cayenne.dba.JdbcPkGenerator)1 ObjAttribute (org.apache.cayenne.map.ObjAttribute)1 ObjEntity (org.apache.cayenne.map.ObjEntity)1 Artist (org.apache.cayenne.testdo.testmap.Artist)1 Test (org.junit.Test)1