Package org.apache.cayenne.map

Examples of org.apache.cayenne.map.DbRelationship$JoinTransformers


                // test one-to-one
                rels = getDbEntity(map, "PAINTING").getRelationships();
                assertNotNull(rels);

                // find relationship to PAINTING_INFO
                DbRelationship oneToOne = null;
                Iterator it = rels.iterator();
                while (it.hasNext()) {
                    DbRelationship rel = (DbRelationship) it.next();
                    if ("PAINTING_INFO".equalsIgnoreCase(rel.getTargetEntityName())) {
                        oneToOne = rel;
                        break;
                    }
                }
View Full Code Here


                    if (pathPart == null) {
                        throw new CayenneRuntimeException(
                                "ObjAttribute has no component: " + oa.getName());
                    }
                    else if (pathPart instanceof DbRelationship) {
                        DbRelationship rel = (DbRelationship) pathPart;
                        dbRelationshipAdded(rel, JoinType.LEFT_OUTER, null);
                    }
                    else if (pathPart instanceof DbAttribute) {
                        DbAttribute dbAttr = (DbAttribute) pathPart;

                        appendColumn(columns, oa, dbAttr, attributes, null);
                    }
                }
                return true;
            }

            public boolean visitToMany(ToManyProperty property) {
                visitRelationship(property);
                return true;
            }

            public boolean visitToOne(ToOneProperty property) {
                visitRelationship(property);
                return true;
            }

            private void visitRelationship(ArcProperty property) {
                resetJoinStack();
               
                ObjRelationship rel = property.getRelationship();
                DbRelationship dbRel = rel.getDbRelationships().get(0);

                List<DbJoin> joins = dbRel.getJoins();
                int len = joins.size();
                for (int i = 0; i < len; i++) {
                    DbJoin join = joins.get(i);
                    DbAttribute src = join.getSource();
                    appendColumn(columns, null, src, attributes, null);
                }
            }
        };

        descriptor.visitAllProperties(visitor);
       
        //stack should be reset, because all root table attributes go with "t0" table alias
        resetJoinStack();

        // add remaining needed attrs from DbEntity
        DbEntity table = getRootDbEntity();
        for (final DbAttribute dba : table.getPrimaryKeys()) {
            appendColumn(columns, null, dba, attributes, null);
        }

        // special handling of a disjoint query...

        if (query instanceof PrefetchSelectQuery) {

            // for each relationship path add PK of the target entity...
            for (String path : ((PrefetchSelectQuery) query).getResultPaths()) {

                Expression pathExp = oe.translateToDbPath(Expression.fromString(path));

                // add joins and find terminating element

                resetJoinStack();

                PathComponent<DbAttribute, DbRelationship> lastComponent = null;
                for (PathComponent<DbAttribute, DbRelationship> component : table
                        .resolvePath(pathExp, getPathAliases())) {

                    if (component.getRelationship() != null) {
                        dbRelationshipAdded(component.getRelationship(), component
                                .getJoinType(), null);
                    }

                    lastComponent = component;
                }

                // process terminating element
                if (lastComponent != null) {

                    DbRelationship relationship = lastComponent.getRelationship();

                    if (relationship != null) {

                        String labelPrefix = pathExp.toString().substring("db:".length());
                        DbEntity targetEntity = (DbEntity) relationship.getTargetEntity();

                        for (DbAttribute pk : targetEntity.getPrimaryKeys()) {

                            // note that we my select a source attribute, but label it as
                            // target for simplified snapshot processing
                            appendColumn(columns, null, pk, attributes, labelPrefix
                                    + '.'
                                    + pk.getName());
                        }
                    }
                }
            }
        }

        // handle joint prefetches directly attached to this query...
        if (query.getPrefetchTree() != null) {

            for (PrefetchTreeNode prefetch : query.getPrefetchTree().adjacentJointNodes()) {

                // for each prefetch add all joins plus columns from the target entity
                Expression prefetchExp = Expression.fromString(prefetch.getPath());
                Expression dbPrefetch = oe.translateToDbPath(prefetchExp);

                resetJoinStack();
                DbRelationship r = null;
                for (PathComponent<DbAttribute, DbRelationship> component : table
                        .resolvePath(dbPrefetch, getPathAliases())) {
                    r = component.getRelationship();
                    dbRelationshipAdded(r, JoinType.LEFT_OUTER, null);
                }

                if (r == null) {
                    throw new CayenneRuntimeException("Invalid joint prefetch '"
                            + prefetch
                            + "' for entity: "
                            + oe.getName());
                }

                // add columns from the target entity, including those that are matched
                // against the FK of the source entity. This is needed to determine
                // whether optional relationships are null

                // go via target OE to make sure that Java types are mapped correctly...
                ObjRelationship targetRel = (ObjRelationship) prefetchExp.evaluate(oe);
                Iterator<ObjAttribute> targetObjAttrs = (Iterator<ObjAttribute>) targetRel
                        .getTargetEntity()
                        .getAttributes()
                        .iterator();

                String labelPrefix = dbPrefetch.toString().substring("db:".length());
                while (targetObjAttrs.hasNext()) {
                    ObjAttribute oa = targetObjAttrs.next();
                    Iterator<CayenneMapEntry> dbPathIterator = oa.getDbPathIterator();
                    while (dbPathIterator.hasNext()) {
                        Object pathPart = dbPathIterator.next();

                        if (pathPart == null) {
                            throw new CayenneRuntimeException(
                                    "ObjAttribute has no component: " + oa.getName());
                        }
                        else if (pathPart instanceof DbRelationship) {
                            DbRelationship rel = (DbRelationship) pathPart;
                            dbRelationshipAdded(rel, JoinType.INNER, null);
                        }
                        else if (pathPart instanceof DbAttribute) {
                            DbAttribute attribute = (DbAttribute) pathPart;
View Full Code Here

            if (rel.isToPK() && !rel.isToDependentPK()) {

                if (getAdapter().supportsUniqueConstraints()) {

                    DbRelationship reverse = rel.getReverseRelationship();
                    if (reverse != null && !reverse.isToMany() && !reverse.isToPK()) {

                        String unique = getAdapter().createUniqueConstraint(
                                (DbEntity) rel.getSourceEntity(),
                                rel.getSourceAttributes());
                        if (unique != null) {
View Full Code Here

            // create a copy of the original PK list,
            // since the list will be modified locally
            List<DbAttribute> pkAttributes = new ArrayList<DbAttribute>(nextEntity
                    .getPrimaryKeys());
            while (pkAttributes.size() > 0 && relationships.hasNext()) {
                DbRelationship nextRelationship = relationships.next();
                if (!nextRelationship.isToMasterPK()) {
                    continue;
                }

                // supposedly all source attributes of the relationship
                // to master entity must be a part of primary key,
                // so
                for (DbJoin join : nextRelationship.getJoins()) {
                    pkAttributes.remove(join.getSource());
                }
            }

            // primary key is needed only if at least one of the primary key attributes
View Full Code Here

                                "No matching objects found for ObjectId "
                                        + object.getObjectId()
                                        + ". Object may have been deleted externally.");
                    }

                    DbRelationship dbRel = rel.getDbRelationships().get(0);
                    for (DbJoin join : dbRel.getJoins()) {
                        String key = join.getSourceName();
                        snapshot.put(key, storedSnapshot.get(key));
                    }

                    return true;
                }

                // target is resolved and we have an FK->PK to it,
                // so extract it from target...
                Persistent target = (Persistent) targetObject;
                Map<String, Object> idParts = target.getObjectId().getIdSnapshot();

                // this may happen in uncommitted objects - see the warning in the JavaDoc
                // of
                // this method.
                if (idParts.isEmpty()) {
                    return true;
                }

                DbRelationship dbRel = rel.getDbRelationships().get(0);
                Map<String, Object> fk = dbRel.srcFkSnapshotWithTargetSnapshot(idParts);
                snapshot.putAll(fk);
                return true;
            }
        });
View Full Code Here

        DbEntity artistDbEntity = map.getDbEntity("ARTIST");
        assertNotNull(artistDbEntity);

        // relation from new_table to artist
        DbRelationship r1 = new DbRelationship("toArtistR1");
        r1.setSourceEntity(dbEntity);
        r1.setTargetEntity(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.setTargetEntity(dbEntity);
        r2.setToMany(true);
        r2.addJoin(new DbJoin(r2, "ARTIST_ID", "ARTIST_ID"));
        artistDbEntity.addRelationship(r2);

        assertTokensAndExecute(node, map, 2, 0);
        assertTokensAndExecute(node, map, 0, 0);

        DataContext ctxt = createDataContext();

        // remove relationships
        dbEntity.removeRelationship(r1.getName());
        artistDbEntity.removeRelationship(r2.getName());
        ctxt.getEntityResolver().clearCache();
        assertTokensAndExecute(node, map, 1, 1);
        assertTokensAndExecute(node, map, 0, 0);

        // clear up
View Full Code Here

        assertTokensAndExecute(node, map, 1, 0);
        assertTokensAndExecute(node, map, 0, 0);

        // relation from new_table to artist
        DbRelationship r1 = new DbRelationship("toArtistR1");
        r1.setSourceEntity(dbEntity);
        r1.setTargetEntity(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.setTargetEntity(dbEntity);
        r2.setToMany(true);
        r2.addJoin(new DbJoin(r2, "ARTIST_ID", "ARTIST_ID"));
        artistDbEntity.addRelationship(r2);

        assertTokensAndExecute(node, map, 1, 0);
        assertTokensAndExecute(node, map, 0, 0);

        DataContext ctxt = createDataContext();

        // remove relationships
        dbEntity.removeRelationship(r1.getName());
        artistDbEntity.removeRelationship(r2.getName());
        ctxt.getEntityResolver().clearCache();
        assertTokensAndExecute(node, map, 1, 1);
        assertTokensAndExecute(node, map, 0, 0);

        // clear up
View Full Code Here

            super(target, toMany);
        }

        @Override
        protected Object create(String name, Object namingContext) {
            return new DbRelationship(name);
        }
View Full Code Here

        dbEntity2.addAttribute(e2col3);

        map.addDbEntity(dbEntity2);

        // create db relationships
        DbRelationship rel1To2 = new DbRelationship("rel1To2");
        rel1To2.setSourceEntity(dbEntity1);
        rel1To2.setTargetEntity(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.setTargetEntity(dbEntity1);
        rel2To1.setToMany(false);
        rel2To1.addJoin(new DbJoin(rel2To1, e2col2.getName(), e1col1.getName()));
        dbEntity2.addRelationship(rel2To1);
        assertSame(rel1To2, rel2To1.getReverseRelationship());
        assertSame(rel2To1, rel1To2.getReverseRelationship());

        assertTokensAndExecute(node, map, 4, 0);
        assertTokensAndExecute(node, map, 0, 0);

        // create ObjEntities
        ObjEntity objEntity1 = new ObjEntity("NewTable");
        objEntity1.setDbEntity(dbEntity1);
        ObjAttribute oatr1 = new ObjAttribute("name");
        oatr1.setDbAttributePath(e1col2.getName());
        oatr1.setType("java.lang.String");
        objEntity1.addAttribute(oatr1);
        map.addObjEntity(objEntity1);
        ObjEntity objEntity2 = new ObjEntity("NewTable2");
        objEntity2.setDbEntity(dbEntity2);
        ObjAttribute o2a1 = new ObjAttribute("name");
        o2a1.setDbAttributePath(e2col3.getName());
        o2a1.setType("java.lang.String");
        objEntity2.addAttribute(o2a1);
        map.addObjEntity(objEntity2);
       
        // create ObjRelationships
        assertEquals(0, objEntity1.getRelationships().size());
        assertEquals(0, objEntity2.getRelationships().size());
        ObjRelationship objRel1To2 = new ObjRelationship("objRel1To2");
        objRel1To2.addDbRelationship(rel1To2);
        objRel1To2.setSourceEntity(objEntity1);
        objRel1To2.setTargetEntity(objEntity2);
        objEntity1.addRelationship(objRel1To2);
        ObjRelationship objRel2To1 = new ObjRelationship("objRel2To1");
        objRel2To1.addDbRelationship(rel2To1);
        objRel2To1.setSourceEntity(objEntity2);
        objRel2To1.setTargetEntity(objEntity1);
        objEntity2.addRelationship(objRel2To1);
        assertEquals(1, objEntity1.getRelationships().size());
        assertEquals(1, objEntity2.getRelationships().size());
        assertSame(objRel1To2, objRel2To1.getReverseRelationship());
        assertSame(objRel2To1, objRel1To2.getReverseRelationship());

        // remove relationship and fk from model, merge to db and read to model
        dbEntity2.removeRelationship(rel2To1.getName());
        dbEntity1.removeRelationship(rel1To2.getName());
        dbEntity2.removeAttribute(e2col2.getName());
        List<MergerToken> tokens = createMergeTokens();
        assertTokens(tokens, 2, 1);
        for (MergerToken token : tokens) {
            if (token.getDirection().isToDb()) {
                execute(token);
            }
        }
        assertTokensAndExecute(0, 0);
        dbEntity2.addRelationship(rel2To1);
        dbEntity1.addRelationship(rel1To2);
        dbEntity2.addAttribute(e2col2);
       
        // try do use the merger to remove the relationship in the model
        tokens = createMergeTokens();
        assertTokens(tokens, 2, 0);
        // TODO: reversing the following two tokens should also reverse the order
        MergerToken token0 = tokens.get(0).createReverse(mergerFactory());
        MergerToken token1 = tokens.get(1).createReverse(mergerFactory());
        assertTrue(token0 instanceof DropColumnToModel);
        assertTrue(token1 instanceof DropRelationshipToModel);
        execute(token1);
        execute(token0);
       
        // check after merging
        assertNull(dbEntity2.getAttribute(e2col2.getName()));
        assertEquals(0, dbEntity1.getRelationships().size());
        assertEquals(0, dbEntity2.getRelationships().size());
        assertEquals(0, objEntity1.getRelationships().size());
        assertEquals(0, objEntity2.getRelationships().size());

        // clear up
        DataContext ctxt = createDataContext();
        dbEntity1.removeRelationship(rel1To2.getName());
        dbEntity2.removeRelationship(rel2To1.getName());
        map.removeObjEntity(objEntity1.getName(), true);
        map.removeDbEntity(dbEntity1.getName(), true);
        map.removeObjEntity(objEntity2.getName(), true);
        map.removeDbEntity(dbEntity2.getName(), true);
        ctxt.getEntityResolver().clearCache();
View Full Code Here

       
        // force drop fk column in db
        execute(mergerFactory().createDropColumnToDb(dbEntity2, e2col2));

        // create db relationships, but do not sync them to db
        DbRelationship rel1To2 = new DbRelationship("rel1To2");
        rel1To2.setSourceEntity(dbEntity1);
        rel1To2.setTargetEntity(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.setTargetEntity(dbEntity1);
        rel2To1.setToMany(false);
        rel2To1.addJoin(new DbJoin(rel2To1, e2col2.getName(), e1col1.getName()));
        dbEntity2.addRelationship(rel2To1);
        assertSame(rel1To2, rel2To1.getReverseRelationship());
        assertSame(rel2To1, rel1To2.getReverseRelationship());

        // create ObjEntities
        ObjEntity objEntity1 = new ObjEntity("NewTable");
        objEntity1.setDbEntity(dbEntity1);
        ObjAttribute oatr1 = new ObjAttribute("name");
        oatr1.setDbAttributePath(e1col2.getName());
        oatr1.setType("java.lang.String");
        objEntity1.addAttribute(oatr1);
        map.addObjEntity(objEntity1);
        ObjEntity objEntity2 = new ObjEntity("NewTable2");
        objEntity2.setDbEntity(dbEntity2);
        ObjAttribute o2a1 = new ObjAttribute("name");
        o2a1.setDbAttributePath(e2col3.getName());
        o2a1.setType("java.lang.String");
        objEntity2.addAttribute(o2a1);
        map.addObjEntity(objEntity2);
       
        // create ObjRelationships
        assertEquals(0, objEntity1.getRelationships().size());
        assertEquals(0, objEntity2.getRelationships().size());
        ObjRelationship objRel1To2 = new ObjRelationship("objRel1To2");
        objRel1To2.addDbRelationship(rel1To2);
        objRel1To2.setSourceEntity(objEntity1);
        objRel1To2.setTargetEntity(objEntity2);
        objEntity1.addRelationship(objRel1To2);
        ObjRelationship objRel2To1 = new ObjRelationship("objRel2To1");
        objRel2To1.addDbRelationship(rel2To1);
        objRel2To1.setSourceEntity(objEntity2);
        objRel2To1.setTargetEntity(objEntity1);
        objEntity2.addRelationship(objRel2To1);
        assertEquals(1, objEntity1.getRelationships().size());
        assertEquals(1, objEntity2.getRelationships().size());
        assertSame(objRel1To2, objRel2To1.getReverseRelationship());
        assertSame(objRel2To1, objRel1To2.getReverseRelationship());

        // try do use the merger to remove the column and relationship in the model
        List<MergerToken> tokens = createMergeTokens();
        assertTokens(tokens, 2, 0);
        // TODO: reversing the following two tokens should also reverse the order
        MergerToken token0 = tokens.get(0).createReverse(mergerFactory());
        MergerToken token1 = tokens.get(1).createReverse(mergerFactory());
        assertTrue(token0.getClass().getName(), token0 instanceof DropColumnToModel);
        assertTrue(token1.getClass().getName(), token1 instanceof DropRelationshipToModel);
        // do not execute DropRelationshipToModel, only DropColumnToModel.
        execute(token0);
       
        // check after merging
        assertNull(dbEntity2.getAttribute(e2col2.getName()));
        assertEquals(0, dbEntity1.getRelationships().size());
        assertEquals(0, dbEntity2.getRelationships().size());
        assertEquals(0, objEntity1.getRelationships().size());
        assertEquals(0, objEntity2.getRelationships().size());

        // clear up
        DataContext ctxt = createDataContext();
        dbEntity1.removeRelationship(rel1To2.getName());
        dbEntity2.removeRelationship(rel2To1.getName());
        map.removeObjEntity(objEntity1.getName(), true);
        map.removeDbEntity(dbEntity1.getName(), true);
        map.removeObjEntity(objEntity2.getName(), true);
        map.removeDbEntity(dbEntity2.getName(), true);
        ctxt.getEntityResolver().clearCache();
View Full Code Here

TOP

Related Classes of org.apache.cayenne.map.DbRelationship$JoinTransformers

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.