Search in sources :

Example 21 with DbRelationship

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

the class DefaultObjectNameGenerator method toOneRelationshipName.

protected String toOneRelationshipName(DbRelationship... relationshipChain) {
    DbRelationship first = relationshipChain[0];
    DbRelationship last = relationshipChain[relationshipChain.length - 1];
    List<DbJoin> joins = first.getJoins();
    if (joins.isEmpty()) {
        // and just return targetName
        return stemmed(last.getTargetEntityName());
    }
    DbJoin join1 = joins.get(0);
    // TODO: multi-join relationships
    // return the name of the FK column sans ID
    String fkColName = join1.getSourceName();
    if (fkColName == null) {
        return stemmed(last.getTargetEntityName());
    } else if (fkColName.toUpperCase().endsWith("_ID") && fkColName.length() > 3) {
        return fkColName.substring(0, fkColName.length() - 3);
    } else if (fkColName.toUpperCase().endsWith("ID") && fkColName.length() > 2) {
        return fkColName.substring(0, fkColName.length() - 2);
    } else {
        return stemmed(last.getTargetEntityName());
    }
}
Also used : DbRelationship(org.apache.cayenne.map.DbRelationship) DbJoin(org.apache.cayenne.map.DbJoin)

Example 22 with DbRelationship

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

the class DefaultObjectNameGenerator method toManyRelationshipName.

protected String toManyRelationshipName(DbRelationship... relationshipChain) {
    DbRelationship last = relationshipChain[relationshipChain.length - 1];
    String baseName = stemmed(last.getTargetEntityName());
    try {
        // by default we use English rules here...
        return Noun.pluralOf(baseName.toLowerCase(), Locale.ENGLISH);
    } catch (Exception inflectorError) {
        // behavior if something's gone wrong
        return baseName;
    }
}
Also used : DbRelationship(org.apache.cayenne.map.DbRelationship)

Example 23 with DbRelationship

use of org.apache.cayenne.map.DbRelationship 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 24 with DbRelationship

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

the class MergerFactoryIT method testAddForeignKeyWithTable.

@Test
public void testAddForeignKeyWithTable() throws Exception {
    dropTableIfPresent("NEW_TABLE");
    assertTokensAndExecute(0, 0);
    DbEntity dbEntity = new DbEntity("NEW_TABLE");
    attr(dbEntity, "ID", Types.INTEGER, true, true);
    attr(dbEntity, "NAME", Types.VARCHAR, false, false).setMaxLength(10);
    attr(dbEntity, "ARTIST_ID", Types.BIGINT, false, false);
    map.addDbEntity(dbEntity);
    DbEntity artistDbEntity = map.getDbEntity("ARTIST");
    assertNotNull(artistDbEntity);
    // relation from new_table to artist
    DbRelationship r1 = new DbRelationship("toArtistR1");
    r1.setSourceEntity(dbEntity);
    r1.setTargetEntityName(artistDbEntity);
    r1.setToMany(false);
    r1.addJoin(new DbJoin(r1, "ARTIST_ID", "ARTIST_ID"));
    dbEntity.addRelationship(r1);
    // relation from artist to new_table
    DbRelationship r2 = new DbRelationship("toNewTableR2");
    r2.setSourceEntity(artistDbEntity);
    r2.setTargetEntityName(dbEntity);
    r2.setToMany(true);
    r2.addJoin(new DbJoin(r2, "ARTIST_ID", "ARTIST_ID"));
    artistDbEntity.addRelationship(r2);
    assertTokensAndExecute(2, 0);
    assertTokensAndExecute(0, 0);
    // remove relationships
    dbEntity.removeRelationship(r1.getName());
    artistDbEntity.removeRelationship(r2.getName());
    resolver.refreshMappingCache();
    /*
         * Db -Rel 'toArtistR1' - NEW_TABLE 1 -> 1 ARTIST"
r2 =     * Db -Rel 'toNewTableR2' - ARTIST 1 -> * NEW_TABLE" -- Not generated any more
         * */
    assertTokensAndExecute(1, 0);
    assertTokensAndExecute(0, 0);
    // clear up
    // map.removeObjEntity(objEntity.getName(), true);
    map.removeDbEntity(dbEntity.getName(), true);
    resolver.refreshMappingCache();
    // assertNull(map.getObjEntity(objEntity.getName()));
    assertNull(map.getDbEntity(dbEntity.getName()));
    assertFalse(map.getDbEntities().contains(dbEntity));
    assertTokensAndExecute(1, 0);
    assertTokensAndExecute(0, 0);
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) DbJoin(org.apache.cayenne.map.DbJoin) Test(org.junit.Test)

Example 25 with DbRelationship

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

the class FindAction method searchInDbEntities.

private void searchInDbEntities(Pattern pattern, List<SearchResultEntry> result, DataMap dataMap) {
    for (DbEntity ent : dataMap.getDbEntities()) {
        if (match(ent.getName(), pattern)) {
            result.add(new SearchResultEntry(ent, ent.getName()));
        }
        for (DbAttribute attr : ent.getAttributes()) {
            if (match(attr.getName(), pattern)) {
                result.add(new SearchResultEntry(attr, ent.getName() + "." + attr.getName()));
            }
        }
        for (DbRelationship rel : ent.getRelationships()) {
            if (match(rel.getName(), pattern)) {
                result.add(new SearchResultEntry(rel, ent.getName() + "." + rel.getName()));
            }
        }
        checkCatalogOrSchema(pattern, result, ent, ent.getCatalog());
        checkCatalogOrSchema(pattern, result, ent, ent.getSchema());
    }
}
Also used : DetectedDbEntity(org.apache.cayenne.map.DetectedDbEntity) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) DbAttribute(org.apache.cayenne.map.DbAttribute)

Aggregations

DbRelationship (org.apache.cayenne.map.DbRelationship)106 DbEntity (org.apache.cayenne.map.DbEntity)59 DbAttribute (org.apache.cayenne.map.DbAttribute)35 DbJoin (org.apache.cayenne.map.DbJoin)35 ObjEntity (org.apache.cayenne.map.ObjEntity)30 ObjRelationship (org.apache.cayenne.map.ObjRelationship)28 ObjAttribute (org.apache.cayenne.map.ObjAttribute)20 Test (org.junit.Test)15 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)13 ArrayList (java.util.ArrayList)11 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)10 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)9 DataMap (org.apache.cayenne.map.DataMap)9 Entity (org.apache.cayenne.map.Entity)8 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)8 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)8 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)8 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)8 HashMap (java.util.HashMap)6 ObjectId (org.apache.cayenne.ObjectId)6