Package org.apache.cayenne.map

Examples of org.apache.cayenne.map.ObjRelationship


        DbAttribute da1 = a1.getDbAttribute();
        assertNotNull(da1);
        assertSame(da1, artistDE.getAttribute("ARTIST_NAME"));

        // check ObjRelationships
        ObjRelationship rel = (ObjRelationship) artistE
                .getRelationship("artistExhibitArray");
        assertNotNull(rel);
        assertEquals(1, rel.getDbRelationships().size());

        // check DbRelationships
        DbRelationship drel = (DbRelationship) artistDE
                .getRelationship("artistExhibitArray");
        assertNotNull(drel);
        assertSame(drel, rel.getDbRelationships().get(0));

        // flattened relationships
        ObjRelationship frel = (ObjRelationship) artistE.getRelationship("exhibitArray");
        assertNotNull(frel);
        assertEquals(2, frel.getDbRelationships().size());

        // storing data map may uncover some inconsistencies
        PrintWriter mockupWriter = new NullPrintWriter();
        map.encodeAsXML(mockupWriter);
    }
View Full Code Here


                }
            }

            // only create obj relationship if it is a class property
            if (classProps.contains(relName)) {
                ObjRelationship rel = new ObjRelationship();
                rel.setName(relName);
                rel.setSourceEntity(objEntity);
                rel.setTargetEntity(target);
                objEntity.addRelationship(rel);

                if (dbRel != null) {
                    rel.addDbRelationship(dbRel);
                }
            }
        }
    }
View Full Code Here

            // ignore normal relationships
            if (targetPath == null) {
                continue;
            }

            ObjRelationship flatRel = new ObjRelationship();
            flatRel.setName((String) relMap.get("name"));
            flatRel.setSourceEntity(e);
           
            try {
                flatRel.setDbRelationshipPath(targetPath);
            } catch (ExpressionException ex) {
                logger.warn("Invalid relationship: " + targetPath);
                continue;
            }

            // find target entity
            Map entityInfo = info;
            StringTokenizer toks = new StringTokenizer(targetPath, ".");
            while (toks.hasMoreTokens() && entityInfo != null) {
                String pathComponent = toks.nextToken();

                // get relationship info and reset entityInfo, so that we could use
                // entityInfo state as an indicator of valid flat relationship enpoint
                // outside the loop
                Collection relationshipInfo = (Collection) entityInfo
                        .get("relationships");
                entityInfo = null;

                if (relationshipInfo == null) {
                    break;
                }

                Iterator rit = relationshipInfo.iterator();
                while (rit.hasNext()) {
                    Map pathRelationship = (Map) rit.next();
                    if (pathComponent.equals(pathRelationship.get("name"))) {
                        String targetName = (String) pathRelationship.get("destination");
                        entityInfo = helper.entityPListMap(targetName);
                        break;
                    }
                }
            }
           
            if(entityInfo != null) {
                flatRel.setTargetEntityName((String) entityInfo.get("name"));
            }
           

            e.addRelationship(flatRel);
        }
View Full Code Here

        // Disallow a Nullify delete rule where the relationship is toMany and the
        // foreign key attributes are mandatory.
        if (relationship.isToMany()
                && !relationship.isFlattened()
                && (relationship.getDeleteRule() == DeleteRule.NULLIFY)) {
            ObjRelationship inverse = relationship.getReverseRelationship();
            if (inverse != null) {
                DbRelationship firstRel = inverse.getDbRelationships().get(0);
                Iterator<DbJoin> attributePairIterator = firstRel.getJoins().iterator();
                // by default, the relation will be check for mandatory.
                boolean check = true;
                while (attributePairIterator.hasNext()) {
                    DbJoin pair = attributePairIterator.next();
                    if (!pair.getSource().isMandatory()) {
                        // a field of the fk can be nullable, cancel the check.
                        check = false;
                        break;
                    }
                }

                if (check) {
                    addFailure(
                            validationResult,
                            relationship,
                            "ObjRelationship '%s' has a Nullify delete rule and a mandatory reverse relationship",
                            toString(relationship));
                }
            }
        }

        // check for relationships with same source and target entities
        ObjEntity entity = (ObjEntity) relationship.getSourceEntity();
        for (ObjRelationship rel : entity.getRelationships()) {
            if (relationship.getDbRelationshipPath() != null && relationship.getDbRelationshipPath().equals(rel.getDbRelationshipPath())) {
                if (relationship != rel &&
                        relationship.getTargetEntity() == rel.getTargetEntity() &&
                        relationship.getSourceEntity() == rel.getSourceEntity()) {
                    addFailure(
                            validationResult,
                            relationship,
                            "ObjectRelationship '%s' duplicates relationship '%s'",
                            toString(relationship),
                            toString(rel));
                }
            }
        }

        // check for invalid relationships in inherited entities
        if (relationship.getReverseRelationship() != null) {
            ObjRelationship revRel = relationship.getReverseRelationship();
            if (relationship.getSourceEntity() != revRel.getTargetEntity()
                    || relationship.getTargetEntity() != revRel.getSourceEntity()) {
                addFailure(
                        validationResult,
                        revRel,
                        "Usage of super entity's relationships '%s' as reversed relationships for sub entity is discouraged",
                        toString(revRel));
View Full Code Here

            RelationshipQuery relationshipQuery = (RelationshipQuery) query;
            if (relationshipQuery.isRefreshing()) {
                return !DONE;
            }

            ObjRelationship relationship = relationshipQuery.getRelationship(domain
                    .getEntityResolver());

            // check if we can derive target PK from FK...
            if (relationship.isSourceIndependentFromTargetChange()) {
                return !DONE;
            }
           
            // we can assume that there is one and only one DbRelationship as
            // we previously checked that "!isSourceIndependentFromTargetChange"
            DbRelationship dbRelationship = relationship.getDbRelationships().get(0);
           
            // FK pointing to a unique field that is a 'fake' PK (CAY-1755)...
            // It is not sufficient to generate target ObjectId.
            DbEntity targetEntity = (DbEntity) dbRelationship.getTargetEntity();
            if (dbRelationship.getJoins().size() < targetEntity
                    .getPrimaryKeys().size()) {
                return !DONE;
            }
           

            if (cache == null) {
                return !DONE;
            }

            DataRow sourceRow = cache.getCachedSnapshot(relationshipQuery.getObjectId());
            if (sourceRow == null) {
                return !DONE;
            }


            ObjectId targetId = sourceRow.createTargetObjectId(
                    relationship.getTargetEntityName(),
                    dbRelationship);

            // null id means that FK is null...
            if (targetId == null) {
                this.response = new GenericResponse(Collections.EMPTY_LIST);
                return DONE;
            }

            DataRow targetRow = cache.getCachedSnapshot(targetId);

            if (targetRow != null) {
                this.response = new GenericResponse(Collections.singletonList(targetRow));
                return DONE;
            }

            // check whether a non-null FK is enough to assume non-null target, and if so,
            // create a fault
            if (context != null
                    && relationship.isSourceDefiningTargetPrecenseAndType(domain
                            .getEntityResolver())) {

                // prevent passing partial snapshots to ObjectResolver per CAY-724.
                // Create a hollow object right here and skip object conversion downstream
                this.noObjectConversion = true;
View Full Code Here

            this.toMany = toMany;
        }

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

            }

            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());
View Full Code Here

        // only include this entity relationships and skip superclasses...
        for (Relationship relationship : descriptor
                .getEntity()
                .getDeclaredRelationships()) {

            ObjRelationship objRelationship = (ObjRelationship) relationship;

            if (relationship.isToMany()) {

                String collectionType = objRelationship.getCollectionType();
                if (collectionType == null
                        || ObjRelationship.DEFAULT_COLLECTION_TYPE.equals(collectionType)) {
                    createToManyListProperty(descriptor, objRelationship);
                }
                else if (collectionType.equals("java.util.Map")) {
View Full Code Here

        // Disallow a Nullify delete rule where the relationship is toMany and the
        // foreign key attributes are mandatory.
        if (relationship.isToMany()
                && !relationship.isFlattened()
                && (relationship.getDeleteRule() == DeleteRule.NULLIFY)) {
            ObjRelationship inverse = relationship.getReverseRelationship();
            if (inverse != null) {
                DbRelationship firstRel = inverse.getDbRelationships().get(0);
                Iterator<DbJoin> attributePairIterator = firstRel.getJoins().iterator();
                // by default, the relation will be check for mandatory.
                boolean check = true;
                while (attributePairIterator.hasNext()) {
                    DbJoin pair = attributePairIterator.next();
                    if (!pair.getSource().isMandatory()) {
                        // a field of the fk can be nullable, cancel the check.
                        check = false;
                        break;
                    }
                }

                if (check) {
                    addFailure(
                            validationResult,
                            relationship,
                            "ObjRelationship '%s' has a Nullify delete rule and a mandatory reverse relationship",
                            toString(relationship));
                }
            }
        }
       
        // check for relationships with same source and target entities
        ObjEntity entity = (ObjEntity) relationship.getSourceEntity();
        for (ObjRelationship rel : entity.getRelationships()) {
            if (relationship.getDbRelationshipPath().equals(rel.getDbRelationshipPath())) {
                if (relationship != rel &&
                        relationship.getTargetEntity() == rel.getTargetEntity() &&
                        relationship.getSourceEntity() == rel.getSourceEntity()) {
                    addFailure(
                            validationResult,
                            relationship,
                            "ObjectRelationship '%s' duplicates relationship '%s'",
                            toString(relationship),
                            toString(rel));
                }
            }
        }
       
        // check for invalid relationships in inherited entities
        if (relationship.getReverseRelationship() != null) {
            ObjRelationship revRel = relationship.getReverseRelationship();
            if (relationship.getSourceEntity() != revRel.getTargetEntity()
                    || relationship.getTargetEntity() != revRel.getSourceEntity()) {
                addFailure(
                        validationResult,
                        revRel,
                        "Usage of super entity's relationships '%s' as reversed relationships for sub entity is discouraged",
                        toString(revRel));
View Full Code Here

        EntityResolver resolver = context.getEntityResolver();
        ObjEntity paintingEntity = resolver.lookupObjEntity(Painting.class);
        ObjEntity galleryEntity = resolver.lookupObjEntity(Gallery.class);
        ObjEntity artistExhibitEntity = resolver.lookupObjEntity(ArtistExhibit.class);
        ObjEntity exhibitEntity = resolver.lookupObjEntity(Exhibit.class);
        ObjRelationship paintingToArtistRel = (ObjRelationship) paintingEntity
                .getRelationship("toArtist");
        paintingEntity.removeRelationship("toArtist");

        ObjRelationship galleryToPaintingRel = (ObjRelationship) galleryEntity
                .getRelationship("paintingArray");
        galleryEntity.removeRelationship("paintingArray");

        ObjRelationship artistExhibitToArtistRel = (ObjRelationship) artistExhibitEntity
                .getRelationship("toArtist");
        artistExhibitEntity.removeRelationship("toArtist");

        ObjRelationship exhibitToArtistExhibitRel = (ObjRelationship) exhibitEntity
                .getRelationship("artistExhibitArray");
        exhibitEntity.removeRelationship("artistExhibitArray");

        Expression e = ExpressionFactory.matchExp("artistName", "artist1");
        SelectQuery q = new SelectQuery("Artist", e);
View Full Code Here

TOP

Related Classes of org.apache.cayenne.map.ObjRelationship

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.