Search in sources :

Example 11 with DbJoin

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

the class OpenBaseMergerTokenFactory method createDropRelationshipToDb.

@Override
public MergerToken createDropRelationshipToDb(final DbEntity entity, final DbRelationship rel) {
    return new DropRelationshipToDb(entity, rel) {

        @Override
        public List<String> createSql(DbAdapter adapter) {
            // FK_NAME form jdbc metadata seem to be wrong. It contain a column name
            // and not the 'relationshipName'
            // TODO: tell openbase developer mail list
            DbEntity source = getEntity();
            DbEntity dest = rel.getTargetEntity();
            // only use the first. See adapter
            // TODO: can we be sure this is the first and same as used by the adapter?
            DbJoin join = rel.getJoins().get(0);
            return Collections.singletonList("delete from _SYS_RELATIONSHIP where " + " source_table = '" + dest.getFullyQualifiedName() + "'" + " and source_column = '" + join.getTargetName() + "'" + " and dest_table = '" + source.getFullyQualifiedName() + "'" + " and dest_column = '" + join.getSourceName() + "'");
        }
    };
}
Also used : DbAdapter(org.apache.cayenne.dba.DbAdapter) DbEntity(org.apache.cayenne.map.DbEntity) DropRelationshipToDb(org.apache.cayenne.dbsync.merge.token.db.DropRelationshipToDb) DbJoin(org.apache.cayenne.map.DbJoin)

Example 12 with DbJoin

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

the class DropColumnToModel method execute.

@Override
public void execute(MergerContext mergerContext) {
    // remove relationships mapped to column. duplicate List to prevent
    // ConcurrentModificationException
    List<DbRelationship> dbRelationships = new ArrayList<>(getEntity().getRelationships());
    for (DbRelationship dbRelationship : dbRelationships) {
        for (DbJoin join : dbRelationship.getJoins()) {
            if (join.getSource() == getColumn() || join.getTarget() == getColumn()) {
                remove(mergerContext.getDelegate(), dbRelationship, true);
            }
        }
    }
    // remove ObjAttribute mapped to same column
    for (ObjEntity objEntity : getEntity().mappedObjEntities()) {
        ObjAttribute objAttribute = objEntity.getAttributeForDbAttribute(getColumn());
        if (objAttribute != null) {
            objEntity.removeAttribute(objAttribute.getName());
            mergerContext.getDelegate().objAttributeRemoved(objAttribute);
        }
    }
    // remove DbAttribute
    getEntity().removeAttribute(getColumn().getName());
    mergerContext.getDelegate().dbAttributeRemoved(getColumn());
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DbRelationship(org.apache.cayenne.map.DbRelationship) ArrayList(java.util.ArrayList) DbJoin(org.apache.cayenne.map.DbJoin)

Example 13 with DbJoin

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

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

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

the class InferRelationshipsControllerBase method createReversRelationship.

public void createReversRelationship(DbEntity eSourse, DbEntity eTarget) {
    InferredRelationship myir = new InferredRelationship();
    for (DbRelationship relationship : eSourse.getRelationships()) {
        for (DbJoin join : relationship.getJoins()) {
            if (((DbEntity) join.getSource().getEntity()).equals(eSourse) && ((DbEntity) join.getTarget().getEntity()).equals(eTarget)) {
                return;
            }
        }
    }
    myir.setSource(eSourse);
    myir.setTarget(eTarget);
    inferredRelationships.add(myir);
}
Also used : DbRelationship(org.apache.cayenne.map.DbRelationship) DbJoin(org.apache.cayenne.map.DbJoin)

Aggregations

DbJoin (org.apache.cayenne.map.DbJoin)49 DbRelationship (org.apache.cayenne.map.DbRelationship)35 DbEntity (org.apache.cayenne.map.DbEntity)24 DbAttribute (org.apache.cayenne.map.DbAttribute)18 ObjRelationship (org.apache.cayenne.map.ObjRelationship)14 ObjAttribute (org.apache.cayenne.map.ObjAttribute)12 ObjEntity (org.apache.cayenne.map.ObjEntity)11 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)9 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)8 ArrayList (java.util.ArrayList)7 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)7 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)7 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)7 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)7 HashMap (java.util.HashMap)6 Test (org.junit.Test)6 QuotingStrategy (org.apache.cayenne.dba.QuotingStrategy)5 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)5 HashSet (java.util.HashSet)4 List (java.util.List)3