Package org.apache.openjpa.jdbc.meta

Examples of org.apache.openjpa.jdbc.meta.ClassMapping


                StringBuilder sql = new StringBuilder();
                sql.append("MAX(").
                    append(sel.getColumnAlias(field.getOrderColumn())).
                    append(")");
                sel.select(sql.toString(), field);
                ClassMapping rel = getDefaultElementMapping(false);
                sel.whereForeignKey(getJoinForeignKey(rel),
                    sm.getObjectId(), field.getDefiningMapping(), store);

                Result res = sel.execute(store, fetch);
                try {
                    res.next();
                    pcoll.getChangeTracker().setNextSequence
                        (res.getInt(field) + 1);
                } finally {
                    res.close();
                }
            }
            sm.storeObjectField(fieldIndex, pcoll);
            return;
        }

        // select data for this sm
        final ClassMapping[] elems = getIndependentElementMappings(true);
        final Joins[] resJoins = new Joins[Math.max(1, elems.length)];
        Union union = store.getSQLFactory().newUnion
            (Math.max(1, elems.length));
        union.select(new Union.Selector() {
            public void select(Select sel, int idx) {
                ClassMapping elem = (elems.length == 0) ? null : elems[idx];
                resJoins[idx] = selectAll(sel, elem, sm, store, fetch,
                    JDBCFetchConfiguration.EAGER_PARALLEL);
            }
        });
View Full Code Here


        if (!sel && asc == null)
            return 0;

        // if this mapping can't select the full pk values, then join to
        // super and recurse
        ClassMapping sup;
        if (!mapping.isPrimaryKeyObjectId(true)) {
            sup = mapping.getJoinablePCSuperclassMapping();
            if (joins == null)
                joins = newJoins();
            joins = mapping.joinSuperclass(joins, false);
            return primaryKeyOperation(sup, sel, asc, joins, aliasOrder);
        }

        Column[] cols = mapping.getPrimaryKeyColumns();
        if (isGrouping()) {
            groupBy(cols, joins);
            return 0;
        }

        PathJoins pj = getJoins(joins, false);
        int seld = 0;
        for (int i = 0; i < cols.length; i++)
            if (columnOperation(cols[i], sel, asc, pj, aliasOrder))
                seld |= 2 << i;

        // if this mapping has not been used in the select yet (and therefore
        // is not joined to anything), but has an other-table superclass that
        // has been used, make sure to join to it
        boolean joined = false;
        for (sup = mapping.getJoinablePCSuperclassMapping(); sup != null;
            mapping = sup, sup = mapping.getJoinablePCSuperclassMapping()) {
            if (sup.getTable() == mapping.getTable())
                continue;

            if (mapping.getTable() != sup.getTable()
                && getTableIndex(mapping.getTable(), pj, false) == -1
                && getTableIndex(sup.getTable(), pj, false) != -1) {
                if (pj == null)
                    pj = (PathJoins) newJoins();
                pj = (PathJoins) mapping.joinSuperclass(pj, false);
                joined = true;
            } else
View Full Code Here

    private void wherePrimaryKey(Object oid, ClassMapping mapping, Joins joins,
        JDBCStore store) {
        // if this mapping's identifiers include something other than
        // the pk values, join to super and recurse
        if (!mapping.isPrimaryKeyObjectId(false)) {
            ClassMapping sup = mapping.getJoinablePCSuperclassMapping();
            if (joins == null)
                joins = newJoins();
            joins = mapping.joinSuperclass(joins, false);
            wherePrimaryKey(oid, sup, joins, store);
            return;
View Full Code Here

            return Boolean.TRUE;
        return Boolean.FALSE;
    }

    public Object toDataStoreValue(Object val, JDBCStore store) {
        ClassMapping type = field.getTypeMapping();
        return type.toDataStoreValue(val, type.getPrimaryKeyColumns(), store);
    }
View Full Code Here

     * Flush the primary key values.
     */
    private void flushPrimaryKey(OpenJPAStateManager sm, ColumnIO io,
        boolean set)
        throws SQLException {
        ClassMapping mapping = (ClassMapping) sm.getMetaData();
        while (mapping.getTable() != getTable())
            mapping = mapping.getPCSuperclassMapping();
        Column[] cols = mapping.getPrimaryKeyColumns();
        Object oid = mapping.useIdClassFromParent() ? sm.getObjectId() : null;
        flushJoinValues(sm, oid, cols, cols, io, set);
    }
View Full Code Here

            return;
        }
        if (set && !canSetAny(io, fromCols.length, false))
            return;

        ClassMapping toMapping = (ClassMapping) to.getMetaData();
        Joinable join;
        Object val;
        for (int i = 0; i < toCols.length; i++) {
            // don't even translate join value if unsettable
            if (set) {
                if (_action == ACTION_INSERT && fromCols[i].isAutoAssigned())
                    continue;
                if (!canSet(io, i, false))
                    continue;
            }

            join = toMapping.assertJoinable(toCols[i]);
            if (oid != null)
                val = join.getJoinValue(oid, toCols[i], (JDBCStore) to.
                    getContext().getStoreManager().getInnermostDelegate());
            else
                val = join.getJoinValue(to, toCols[i], (JDBCStore) to.
View Full Code Here

    }

    private void checkObjectPathInheritanceTypeJoined(PathExpState pstate) {
        // if this mapping is in InheritanceType.JOINED,
        // then add joins
         ClassMapping base = _class;
         while (base.getJoinablePCSuperclassMapping() != null)
             base = base.getJoinablePCSuperclassMapping();
         if (base != _class) {
             ClassMapping from = _class;
             ClassMapping to = base;
             _type = OBJECT_PATH;
             for (; from != null && from != to; from = from.getJoinablePCSuperclassMapping()) {
                 pstate.joins = from.joinSuperclass(pstate.joins, false);
             }
         }
View Full Code Here

    public ExpState initialize(Select sel, ExpContext ctx, int flags) {
        PathExpState pstate = new PathExpState(sel.newJoins());
        boolean key = false;
        boolean forceOuter = false;
        ClassMapping rel = _candidate;

        sel.setSchemaAlias(_schemaAlias);

        // iterate to the final field
        ClassMapping owner;
        ClassMapping from, to;
        Action action;
        Variable var;
        Iterator itr = (_actions == null) ? null : _actions.iterator();
        FieldMapping field = null;
        Action prevaction = null;
        boolean isCorrelatedPath = false;
        boolean fromParentRootInSubselect = navigateFromParentRootInSubselect(sel);
               
        while (itr != null && itr.hasNext()) {
            action = (Action) itr.next();

            // treat subqueries like variables for alias generation purposes
            if (action.op == Action.VAR) {
                if (sel.getParent() != null && action.var != null &&
                    prevaction != null && prevaction.data != null &&
                    sel.ctx().getVariable(action.var) == null) {
                    isCorrelatedPath = true;
                    pstate.joins = pstate.joins.setCorrelatedVariable(action.var);
                } else
                    pstate.joins = pstate.joins.setVariable((String) action.data);
            }
            else if (action.op == Action.SUBQUERY) {
                pstate.joins = pstate.joins.setSubselect((String) action.data);
            }
            else if (action.op == Action.UNBOUND_VAR) {
                // unbound vars are cross-joined to the candidate table
                var = (Variable) action.data;
                rel = (ClassMapping) var.getMetaData();
                if (rel == null)
                  throw new IllegalArgumentException(_loc.get(
                      "invalid-unbound-var", var.getName()).toString());
                     
                if (sel.getParent() != null && action.var != null &&
                    sel.ctx().getVariable(action.var) == null) {
                    //System.out.println("Correlated action var="+action.var);
                    isCorrelatedPath = true;
                    pstate.joins = pstate.joins.setCorrelatedVariable(var.getName());
                } else                
                    pstate.joins = pstate.joins.setVariable(var.getName());

                pstate.joins = pstate.joins.crossJoin(_candidate.getTable(),
                    rel.getTable());
                if (!itr.hasNext() && isVariable()) {
                    checkObjectPathInheritanceTypeJoined(pstate);
                }
            } else {
                // move past the previous field, if any
                field = (FieldMapping) ((action.op == Action.GET_XPATH) ?
                    _xmlfield : action.data);

                if (pstate.field != null) {
                    // if this is the second-to-last field and the last is
                    // the related field this field joins to, no need to
                    // traverse: just use this field's fk columns
                    if (!itr.hasNext() && (flags & JOIN_REL) == 0
                        && isJoinedField(pstate.field, key, field)) {
                        pstate.cmpfield = field;
                        break;
                    }
                   
                    if (fromParentRootInSubselect) {
                        isCorrelatedPath = true;
                        pstate.joins = pstate.joins.setCorrelatedVariable(_schemaAlias);
                        pstate.joins.setJoinContext(null);
                    }
                   
                    rel = traverseField(pstate, key, forceOuter ||
                              ctx.store.getDBDictionary().fullResultCollectionInOrderByRelation, false);
                }

                // mark if the next traversal should go through
                // the key rather than value
                key = action.op == Action.GET_KEY;
                forceOuter |= action.op == Action.GET_OUTER;
               
                if (key && itr.hasNext())
                    _keyPath = true;

                // get mapping for the current field
                pstate.field = field;

                owner = pstate.field.getDefiningMapping();
                if (pstate.field.getManagement()
                    != FieldMapping.MANAGE_PERSISTENT)
                    throw new UserException(_loc.get("non-pers-field",
                        pstate.field));

                // find the most-derived type between the declared relation
                // type and the field's owner, and join from that type to
                // the lesser derived type
                if (rel != owner && rel != null) {
                    if (rel.getDescribedType().isAssignableFrom
                        (owner.getDescribedType())) {
                        from = owner;
                        to = rel;
                    } else {
                        from = rel;
                        to = owner;
                    }

                    for (; from != null && from != to;
                        from = from.getJoinablePCSuperclassMapping()) {
                      FieldMapping cast = from.getFieldMapping(pstate.field
                          .getName());
                      if (cast != null)
                        pstate.field = cast;
                        pstate.joins = from.joinSuperclass(pstate.joins, false);
                    }
                }
                // nothing more to do from here on as we encountered an xpath
                // action
                if (action.op == Action.GET_XPATH)
View Full Code Here

    }

    public void selectColumns(Select sel, ExpContext ctx, ExpState state,
        boolean pks) {
        sel.setSchemaAlias(_schemaAlias);
        ClassMapping mapping = getClassMapping(state);
        PathExpState pstate = (PathExpState) state;
        if (_type != OBJECT_PATH && (mapping == null || !pstate.joinedRel ||
            pstate.isEmbedElementColl))
            sel.select(getColumns(state), pstate.joins);
        else if (_key && pstate.field.getKey().isEmbedded())
            selectEmbeddedMapKey(sel, ctx, state);
        else if (pks)
            sel.select(mapping.getPrimaryKeyColumns(), pstate.joins);
        else {
            // select the mapping; allow any subs because we know this must
            // be either a relation, in which case it will already be
            // constrained by the joins, or 'this', in which case the
            // JDBCExpressionFactory takes care of adding class conditions for
View Full Code Here

                JDBCFetchConfiguration.EAGER_NONE, sel.outer(pstate.joins));
        }
    }

    public void groupBy(Select sel, ExpContext ctx, ExpState state) {
        ClassMapping mapping = getClassMapping(state);
        PathExpState pstate = (PathExpState) state;
        if (mapping == null || !pstate.joinedRel)
            sel.groupBy(getColumns(state), sel.outer(pstate.joins));
        else {
            int subs = (_type == UNBOUND_VAR) ? Select.SUBS_JOINABLE
View Full Code Here

TOP

Related Classes of org.apache.openjpa.jdbc.meta.ClassMapping

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.