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);
}
}
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());
}
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());
}
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());
}
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());
}
Aggregations