Search in sources :

Example 46 with DbEntity

use of org.apache.cayenne.map.DbEntity in project cayenne by apache.

the class RelationshipLoader method load.

@Override
public void load(DatabaseMetaData metaData, DbLoadDataStore map) throws SQLException {
    if (config.isSkipRelationshipsLoading()) {
        return;
    }
    for (Map.Entry<String, Set<ExportedKey>> entry : map.getExportedKeysEntrySet()) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Process keys for: " + entry.getKey());
        }
        Set<ExportedKey> exportedKeys = entry.getValue();
        ExportedKey key = exportedKeys.iterator().next();
        if (key == null) {
            throw new IllegalStateException();
        }
        ExportedKey.KeyData PK = key.getPk();
        ExportedKey.KeyData FK = key.getFk();
        DbEntity pkEntity = map.getDbEntity(PK.getTable());
        DbEntity fkEntity = map.getDbEntity(FK.getTable());
        if (pkEntity == null || fkEntity == null) {
            // Check for existence of this entities were made in creation of ExportedKey
            throw new IllegalStateException();
        }
        if (!new EqualsBuilder().append(pkEntity.getCatalog(), PK.getCatalog()).append(pkEntity.getSchema(), PK.getSchema()).append(fkEntity.getCatalog(), FK.getCatalog()).append(fkEntity.getSchema(), PK.getSchema()).isEquals()) {
            LOGGER.info("Skip relation: '" + key + "' because it related to objects from other catalog/schema");
            LOGGER.info("     relation primary key: '" + PK.getCatalog() + "." + PK.getSchema() + "'");
            LOGGER.info("       primary key entity: '" + pkEntity.getCatalog() + "." + pkEntity.getSchema() + "'");
            LOGGER.info("     relation foreign key: '" + FK.getCatalog() + "." + FK.getSchema() + "'");
            LOGGER.info("       foreign key entity: '" + fkEntity.getCatalog() + "." + fkEntity.getSchema() + "'");
            continue;
        }
        // forwardRelationship is a reference from table with primary key
        // it is what exactly we load from db
        DbRelationship forwardRelationship = new DbRelationship();
        forwardRelationship.setSourceEntity(pkEntity);
        forwardRelationship.setTargetEntityName(fkEntity);
        // TODO: dirty and non-transparent... using DbRelationshipDetected for the benefit of the merge package.
        // This info is available from joins....
        DbRelationshipDetected reverseRelationship = new DbRelationshipDetected();
        reverseRelationship.setFkName(FK.getName());
        reverseRelationship.setSourceEntity(fkEntity);
        reverseRelationship.setTargetEntityName(pkEntity);
        reverseRelationship.setToMany(false);
        createAndAppendJoins(exportedKeys, pkEntity, fkEntity, forwardRelationship, reverseRelationship);
        boolean toDependentPK = isToDependentPK(forwardRelationship);
        boolean toMany = isToMany(toDependentPK, fkEntity, forwardRelationship);
        forwardRelationship.setToDependentPK(toDependentPK);
        forwardRelationship.setToMany(toMany);
        // set relationship names only after their joins are ready ...
        // generator logic is based on relationship state...
        setRelationshipName(pkEntity, forwardRelationship);
        setRelationshipName(fkEntity, reverseRelationship);
        checkAndAddRelationship(pkEntity, forwardRelationship);
        checkAndAddRelationship(fkEntity, reverseRelationship);
    }
}
Also used : Set(java.util.Set) EqualsBuilder(org.apache.cayenne.util.EqualsBuilder) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) Map(java.util.Map)

Example 47 with DbEntity

use of org.apache.cayenne.map.DbEntity in project cayenne by apache.

the class DataMapMergerTest method testAddRelationship1.

@Test
public void testAddRelationship1() throws Exception {
    DataMap existing = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt(), dbAttr("attr02").typeInt()), dbEntity("table2").attributes(dbAttr("attr01").typeInt().primaryKey(), dbAttr("attr02").typeInt().primaryKey(), dbAttr("attr03").typeInt().primaryKey())).join("rel", "table1.attr01", "table2.attr01").join("rel1", "table1.attr01", "table2.attr03").build();
    DataMap db = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt(), dbAttr("attr02").typeInt()), dbEntity("table2").attributes(dbAttr("attr01").typeInt().primaryKey(), dbAttr("attr02").typeInt().primaryKey(), dbAttr("attr03").typeInt().primaryKey())).join("rel", "table1.attr01", "table2.attr02").join("rel1", "table1.attr01", "table2.attr03").build();
    List<MergerToken> tokens = dbMerger().createMergeTokens(existing, db);
    assertEquals(2, tokens.size());
    DbEntity entity = existing.getDbEntity("table1");
    assertEquals(factory().createDropRelationshipToDb(entity, entity.getRelationship("rel")).getTokenValue(), tokens.get(0).getTokenValue());
    entity = db.getDbEntity("table1");
    assertEquals(factory().createAddRelationshipToDb(entity, entity.getRelationship("rel")).getTokenValue(), tokens.get(0).getTokenValue());
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 48 with DbEntity

use of org.apache.cayenne.map.DbEntity in project cayenne by apache.

the class DataMapMergerTest method testAddColumn.

@Test
public void testAddColumn() throws Exception {
    DataMap existing = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt(), dbAttr("attr02").typeInt())).build();
    DataMap db = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt())).build();
    List<MergerToken> tokens = dbMerger().createMergeTokens(existing, db);
    assertEquals(1, tokens.size());
    DbEntity entity = existing.getDbEntity("table1");
    assertEquals(factory().createAddColumnToDb(entity, entity.getAttribute("attr02")).getTokenValue(), tokens.get(0).getTokenValue());
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 49 with DbEntity

use of org.apache.cayenne.map.DbEntity in project cayenne by apache.

the class DataMapMergerTest method testChangeColumnLength.

@Test
public void testChangeColumnLength() throws Exception {
    DataMap existing = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeVarchar(60))).build();
    DataMap db = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeVarchar(30))).build();
    List<MergerToken> tokens = dbMerger().createMergeTokens(existing, db);
    assertEquals(1, tokens.size());
    DbEntity entity = existing.getDbEntity("table1");
    DbEntity entityDb = db.getDbEntity("table1");
    assertTrue(tokens.get(0) instanceof SetColumnTypeToDb);
    assertEquals(factory().createSetColumnTypeToDb(entity, entityDb.getAttribute("attr01"), entity.getAttribute("attr01")).getTokenValue(), tokens.get(0).getTokenValue());
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) SetColumnTypeToDb(org.apache.cayenne.dbsync.merge.token.db.SetColumnTypeToDb) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 50 with DbEntity

use of org.apache.cayenne.map.DbEntity in project cayenne by apache.

the class DataMapMergerTest method testRemoveRelationship.

@Test
public void testRemoveRelationship() throws Exception {
    DataMap existing = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt(), dbAttr("attr02").typeInt()), dbEntity("table2").attributes(dbAttr("attr01").typeInt().primaryKey(), dbAttr("attr02").typeInt())).build();
    DataMap db = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt(), dbAttr("attr02").typeInt()), dbEntity("table2").attributes(dbAttr("attr01").typeInt().primaryKey(), dbAttr("attr02").typeInt())).join("rel", "table1.attr01", "table2.attr01").build();
    List<MergerToken> tokens = dbMerger().createMergeTokens(existing, db);
    assertEquals(1, tokens.size());
    DbEntity entity = db.getDbEntity("table1");
    assertEquals(factory().createDropRelationshipToDb(entity, entity.getRelationship("rel")).getTokenValue(), tokens.get(0).getTokenValue());
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Aggregations

DbEntity (org.apache.cayenne.map.DbEntity)273 DbAttribute (org.apache.cayenne.map.DbAttribute)106 Test (org.junit.Test)106 ObjEntity (org.apache.cayenne.map.ObjEntity)64 DbRelationship (org.apache.cayenne.map.DbRelationship)55 DataMap (org.apache.cayenne.map.DataMap)47 ObjAttribute (org.apache.cayenne.map.ObjAttribute)26 ArrayList (java.util.ArrayList)25 DbJoin (org.apache.cayenne.map.DbJoin)24 MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)20 ObjRelationship (org.apache.cayenne.map.ObjRelationship)19 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)16 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)16 Entity (org.apache.cayenne.map.Entity)16 List (java.util.List)15 DbAdapter (org.apache.cayenne.dba.DbAdapter)15 EntityEvent (org.apache.cayenne.map.event.EntityEvent)14 HashMap (java.util.HashMap)12 SelectQuery (org.apache.cayenne.query.SelectQuery)12 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)11