Search in sources :

Example 71 with DbRelationship

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

the class AshwoodEntitySorter method doIndexSorter.

/**
 * Reindexes internal sorter without synchronization.
 */
protected void doIndexSorter() {
    Map<DbEntity, List<DbRelationship>> reflexiveDbEntities = new HashMap<>();
    Digraph<DbEntity, List<DbAttribute>> referentialDigraph = new MapDigraph<>();
    if (entityResolver != null) {
        for (DbEntity entity : entityResolver.getDbEntities()) {
            referentialDigraph.addVertex(entity);
        }
    }
    for (DbEntity destination : entityResolver.getDbEntities()) {
        for (DbRelationship candidate : destination.getRelationships()) {
            if ((!candidate.isToMany() && !candidate.isToDependentPK()) || candidate.isToMasterPK()) {
                DbEntity origin = candidate.getTargetEntity();
                boolean newReflexive = destination.equals(origin);
                for (DbJoin join : candidate.getJoins()) {
                    DbAttribute targetAttribute = join.getTarget();
                    if (targetAttribute.isPrimaryKey()) {
                        if (newReflexive) {
                            List<DbRelationship> reflexiveRels = reflexiveDbEntities.get(destination);
                            if (reflexiveRels == null) {
                                reflexiveRels = new ArrayList<>(1);
                                reflexiveDbEntities.put(destination, reflexiveRels);
                            }
                            reflexiveRels.add(candidate);
                            newReflexive = false;
                        }
                        List<DbAttribute> fks = referentialDigraph.getArc(origin, destination);
                        if (fks == null) {
                            fks = new ArrayList<>();
                            referentialDigraph.putArc(origin, destination, fks);
                        }
                        fks.add(targetAttribute);
                    }
                }
            }
        }
    }
    StrongConnection<DbEntity, List<DbAttribute>> contractor = new StrongConnection<>(referentialDigraph);
    Digraph<Collection<DbEntity>, Collection<List<DbAttribute>>> contractedReferentialDigraph = new MapDigraph<>();
    contractor.contract(contractedReferentialDigraph);
    IndegreeTopologicalSort<Collection<DbEntity>> sorter = new IndegreeTopologicalSort<>(contractedReferentialDigraph);
    Map<DbEntity, ComponentRecord> components = new HashMap<>(contractedReferentialDigraph.order());
    int componentIndex = 0;
    while (sorter.hasNext()) {
        Collection<DbEntity> component = sorter.next();
        ComponentRecord rec = new ComponentRecord(componentIndex++, component);
        for (DbEntity table : component) {
            components.put(table, rec);
        }
    }
    this.reflexiveDbEntities = reflexiveDbEntities;
    this.components = components;
}
Also used : HashMap(java.util.HashMap) DbAttribute(org.apache.cayenne.map.DbAttribute) StrongConnection(org.apache.cayenne.ashwood.graph.StrongConnection) IndegreeTopologicalSort(org.apache.cayenne.ashwood.graph.IndegreeTopologicalSort) MapDigraph(org.apache.cayenne.ashwood.graph.MapDigraph) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) DbJoin(org.apache.cayenne.map.DbJoin) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List)

Example 72 with DbRelationship

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

the class DbRelationshipHandler method createRelationship.

private void createRelationship(Attributes attributes) throws SAXException {
    String name = attributes.getValue("name");
    if (name == null) {
        throw new SAXException("DbRelationshipHandler::createRelationship() - missing \"name\" attribute.");
    }
    String sourceName = attributes.getValue("source");
    if (sourceName == null) {
        throw new SAXException("DbRelationshipHandler::createRelationship() - null source entity");
    }
    DbEntity source = map.getDbEntity(sourceName);
    if (source == null) {
        return;
    }
    dbRelationship = new DbRelationship(name);
    dbRelationship.setSourceEntity(source);
    dbRelationship.setTargetEntityName(attributes.getValue("target"));
    dbRelationship.setToMany(DataMapHandler.TRUE.equalsIgnoreCase(attributes.getValue("toMany")));
    dbRelationship.setToDependentPK(DataMapHandler.TRUE.equalsIgnoreCase(attributes.getValue("toDependentPK")));
    source.addRelationship(dbRelationship);
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) SAXException(org.xml.sax.SAXException)

Example 73 with DbRelationship

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

the class EntityMergeSupport method getAttributesToAdd.

/**
 * Returns a list of attributes that exist in the DbEntity, but are missing
 * from the ObjEntity.
 */
private List<DbAttribute> getAttributesToAdd(ObjEntity objEntity) {
    DbEntity dbEntity = objEntity.getDbEntity();
    List<DbAttribute> missing = new ArrayList<>();
    Collection<DbRelationship> incomingRels = getIncomingRelationships(dbEntity);
    for (DbAttribute dba : dbEntity.getAttributes()) {
        if (shouldAddToObjEntity(objEntity, dba, incomingRels)) {
            missing.add(dba);
        }
    }
    return missing;
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) DbAttribute(org.apache.cayenne.map.DbAttribute) ArrayList(java.util.ArrayList)

Example 74 with DbRelationship

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

the class EntityMergeSupportIT method testMerging.

@Test
public void testMerging() {
    DbEntity dbEntity1 = new DbEntity("NEW_TABLE");
    DbAttribute e1col1 = new DbAttribute("ID", Types.INTEGER, dbEntity1);
    e1col1.setMandatory(true);
    e1col1.setPrimaryKey(true);
    dbEntity1.addAttribute(e1col1);
    DbAttribute e1col2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity1);
    e1col2.setMaxLength(10);
    e1col2.setMandatory(false);
    dbEntity1.addAttribute(e1col2);
    map.addDbEntity(dbEntity1);
    DbEntity dbEntity2 = new DbEntity("NEW_TABLE2");
    DbAttribute e2col1 = new DbAttribute("ID", Types.INTEGER, dbEntity2);
    e2col1.setMandatory(true);
    e2col1.setPrimaryKey(true);
    dbEntity2.addAttribute(e2col1);
    DbAttribute e2col2 = new DbAttribute("FK", Types.INTEGER, dbEntity2);
    dbEntity2.addAttribute(e2col2);
    map.addDbEntity(dbEntity2);
    // create db relationships
    DbRelationship rel1To2 = new DbRelationship("rel1To2");
    rel1To2.setSourceEntity(dbEntity1);
    rel1To2.setTargetEntityName(dbEntity2);
    rel1To2.setToMany(true);
    rel1To2.addJoin(new DbJoin(rel1To2, e1col1.getName(), e2col2.getName()));
    dbEntity1.addRelationship(rel1To2);
    DbRelationship rel2To1 = new DbRelationship("rel2To1");
    rel2To1.setSourceEntity(dbEntity2);
    rel2To1.setTargetEntityName(dbEntity1);
    rel2To1.setToMany(false);
    rel2To1.addJoin(new DbJoin(rel2To1, e2col2.getName(), e1col1.getName()));
    dbEntity2.addRelationship(rel2To1);
    assertSame(rel1To2, rel2To1.getReverseRelationship());
    assertSame(rel2To1, rel1To2.getReverseRelationship());
    ObjEntity objEntity1 = new ObjEntity("NewTable");
    objEntity1.setDbEntity(dbEntity1);
    map.addObjEntity(objEntity1);
    ObjEntity objEntity2 = new ObjEntity("NewTable2");
    objEntity2.setDbEntity(dbEntity2);
    map.addObjEntity(objEntity2);
    EntityMergeSupport entityMergeSupport = new EntityMergeSupport(new DefaultObjectNameGenerator(NoStemStemmer.getInstance()), NamePatternMatcher.EXCLUDE_ALL, true, true, false);
    assertTrue(entityMergeSupport.synchronizeWithDbEntities(Arrays.asList(objEntity1, objEntity2)));
    assertNotNull(objEntity1.getAttribute("name"));
    assertNotNull(objEntity1.getRelationship("newTable2s"));
    assertNotNull(objEntity2.getRelationship("newTable"));
    assertEquals(objEntity1.getRelationship("newTable2s").getDeleteRule(), DeleteRule.DEFAULT_DELETE_RULE_TO_MANY);
    assertEquals(objEntity2.getRelationship("newTable").getDeleteRule(), DeleteRule.DEFAULT_DELETE_RULE_TO_ONE);
    map.removeObjEntity(objEntity2.getName());
    map.removeObjEntity(objEntity1.getName());
    map.removeDbEntity(dbEntity2.getName());
    map.removeDbEntity(dbEntity1.getName());
}
Also used : DefaultObjectNameGenerator(org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator) EntityMergeSupport(org.apache.cayenne.dbsync.merge.context.EntityMergeSupport) ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) DbAttribute(org.apache.cayenne.map.DbAttribute) DbJoin(org.apache.cayenne.map.DbJoin) Test(org.junit.Test)

Example 75 with DbRelationship

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

the class MergerFactoryIT method testAddForeignKeyAfterTable.

@Test
public void testAddForeignKeyAfterTable() 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);
    assertTokensAndExecute(1, 0);
    assertTokensAndExecute(0, 0);
    // 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(1, 0);
    assertTokensAndExecute(0, 0);
    // remove relationships
    dbEntity.removeRelationship(r1.getName());
    artistDbEntity.removeRelationship(r2.getName());
    resolver.refreshMappingCache();
    /*
        * Add Relationship ARTIST->NEW_TABLE To Model -- Not generated any more
        * Drop Relationship NEW_TABLE->ARTIST To DB
        * */
    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)

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