Package org.apache.cayenne.map

Examples of org.apache.cayenne.map.ObjRelationship


    public ClassDescriptor getRootDescriptor() {
        return rootId != null ? getEntityDescriptor(rootId) : null;
    }

    public List<DbRelationship> getIncomingRelationships(String identifier) {
        ObjRelationship relationship = incomingById.get(identifier);
        if (relationship == null) {
            return Collections.emptyList();
        }

        return relationship.getDbRelationships();
    }
View Full Code Here


                .getEntityResolver();
        ObjEntity sourceEntity = resolver.getObjEntity(relationshipOwner
                .getObjectId()
                .getEntityName());

        ObjRelationship relationship = (ObjRelationship) sourceEntity
                .getRelationship(relationshipName);
        ObjRelationship reverse = relationship.getReverseRelationship();

        if (reverse != null && !reverse.isToMany()) {
            Property property = resolver.getClassDescriptor(
                    reverse.getSourceEntity().getName()).getProperty(reverse.getName());

            Iterator it = resolved.iterator();
            while (it.hasNext()) {
                property.writePropertyDirectly(it.next(), null, relationshipOwner);
            }
View Full Code Here

                    String pathChunk = path.getChild(i).getText();
                    buffer.append('.').append(pathChunk);

                    Property property = descriptor.getProperty(pathChunk);
                    if (property instanceof ArcProperty) {
                        ObjRelationship incoming = ((ArcProperty) property)
                                .getRelationship();
                        descriptor = ((ArcProperty) property).getTargetDescriptor();
                        String pathString = buffer.substring(0, buffer.length());

                        descriptorsById.put(pathString, descriptor);
View Full Code Here

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

            public boolean visitToOne(ToOneProperty property) {
                ObjRelationship rel = property.getRelationship();
                DbRelationship dbRel = rel.getDbRelationships().get(0);

                for (DbJoin join : dbRel.getJoins()) {
                    DbAttribute src = join.getSource();
                    if (src.isForeignKey() && visited.add(src.getName())) {
                        entityResult.addDbField(src.getName(), prefix + index[0]++);
View Full Code Here

                visitRelationship(property);
                return true;
            }

            private void visitRelationship(ArcProperty property) {
                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);

        // 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...

        // TODO, Andrus 11/17/2005 - resultPath mechanism is generic and should probably
        // be moved in the superclass (SelectQuery), replacing customDbAttributes.

        if (query instanceof PrefetchSelectQuery) {

            // for each relationship path add closest FK or PK, for each attribute path,
            // add specified column
            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())) {

                    // do not add join for the last DB Rel
                    if (component.getRelationship() != null && !component.isLast()) {
                        dbRelationshipAdded(component.getRelationship(), component
                                .getJoinType(), null);
                    }

                    lastComponent = component;
                }

                String labelPrefix = pathExp.toString().substring("db:".length());

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

                    DbRelationship relationship = lastComponent.getRelationship();

                    if (relationship != null) {

                        // add last join
                        if (relationship.isToMany()) {
                            dbRelationshipAdded(relationship, JoinType.INNER, null);
                        }

                        for (DbJoin j : relationship.getJoins()) {

                            DbAttribute attribute = relationship.isToMany() ? j
                                    .getTarget() : j.getSource();

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

                    else {

                        // label prefix already includes relationship name
                        appendColumn(
                                columns,
                                null,
                                lastComponent.getAttribute(),
                                attributes,
                                labelPrefix);
                    }

                }
            }
        }

        // 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

            if (component.isAlias()) {
                joinSplitAlias = component.getName();
                for (PathComponent<ObjAttribute, ObjRelationship> aliasPart : component
                        .getAliasedPath()) {

                    ObjRelationship relationship = aliasPart.getRelationship();

                    if (relationship == null) {
                        throw new IllegalStateException(
                                "Non-relationship aliased path part: "
                                        + aliasPart.getName());
                    }

                    if (aliasPart.isLast() && component.isLast()) {
                        processRelTermination(
                                relationship,
                                aliasPart.getJoinType(),
                                joinSplitAlias);
                    }
                    else {
                        // find and add joins ....
                        for (DbRelationship dbRel : relationship.getDbRelationships()) {
                            queryAssembler.dbRelationshipAdded(dbRel, aliasPart
                                    .getJoinType(), joinSplitAlias);
                        }
                    }
                }

                continue;
            }

            ObjRelationship relationship = component.getRelationship();
            ObjAttribute attribute = component.getAttribute();

            if (relationship != null) {

                // if this is a last relationship in the path,
                // it needs special handling
                if (component.isLast()) {
                    processRelTermination(
                            relationship,
                            component.getJoinType(),
                            joinSplitAlias);
                }
                else {
                    // find and add joins ....
                    for (DbRelationship dbRel : relationship.getDbRelationships()) {
                        queryAssembler.dbRelationshipAdded(
                                dbRel,
                                component.getJoinType(),
                                joinSplitAlias);
                    }
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

     * Initializes reverse relationship from object <code>val</code> to this object.
     *
     * @param relName name of relationship from this object to <code>val</code>.
     */
    protected void setReverseRelationship(String relName, DataObject val) {
        ObjRelationship rel = (ObjRelationship) objectContext
                .getEntityResolver()
                .getObjEntity(objectId.getEntityName())
                .getRelationship(relName);
        ObjRelationship revRel = rel.getReverseRelationship();
        if (revRel != null) {
            if (revRel.isToMany())
                val.addToManyTarget(revRel.getName(), this, false);
            else
                val.setToOneTarget(revRel.getName(), this, false);
        }
    }
View Full Code Here

        if (entity == null) {
            throw new IllegalStateException("DataObject's entity is unmapped, objectId: "
                    + objectId);
        }

        ObjRelationship rel = (ObjRelationship) entity.getRelationship(relName);
        ObjRelationship revRel = rel.getReverseRelationship();
        if (revRel != null) {
            if (revRel.isToMany())
                val.removeToManyTarget(revRel.getName(), this, false);
            else
                val.setToOneTarget(revRel.getName(), null, false);
        }
    }
View Full Code Here

    protected void assertOneWayRelationships(DataMap map) throws Exception {
        // assert that one way relationships are loaded properly
        // - Db loaded as two-way, obj - as one-way

        ObjEntity exhibitEntity = map.getObjEntity("Exhibit");
        ObjRelationship toTypeObject = (ObjRelationship) exhibitEntity
                .getRelationship("toExhibitType");
        DbRelationship toTypeDB = (DbRelationship) exhibitEntity
                .getDbEntity()
                .getRelationship("toExhibitType");
        assertNotNull(toTypeObject);
        assertNotNull(toTypeDB);
        assertNull(toTypeObject.getReverseRelationship());
        assertNotNull(toTypeDB.getReverseRelationship());
    }
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.