public void delete(OpenJPAStateManager sm, JDBCStore store, RowManager rm) throws SQLException {
    if (field.getMappedBy() != null) return;

    // if nullable, null any existing inverse columns that refer to this obj
    ValueMapping elem = field.getElementMapping();
    ColumnIO io = elem.getColumnIO();
    ForeignKey fk = elem.getForeignKey();
    if (!elem.getUseClassCriteria() && io.isAnyUpdatable(fk, true)) {
      assertInversable();
      Row row = rm.getAllRows(fk.getTable(), Row.ACTION_UPDATE);
      row.setForeignKey(fk, io, null);
      row.whereForeignKey(fk, sm);
      rm.flushAllRows(row);
      return;
    }

    if (!sm.getLoaded().get(field.getIndex())) return;

    // update fk on each field value row
    ClassMapping rel = field.getElementMapping().getTypeMapping();
    StoreContext ctx = store.getContext();
    Collection objs = toCollection(sm.fetchObject(field.getIndex()));
    if (objs != null && !objs.isEmpty())
      for (Iterator itr = objs.iterator(); itr.hasNext(); )
        updateInverse(ctx, itr.next(), rel, rm, sm, 0);
  }
  private void insert(OpenJPAStateManager sm, RowManager rm, Object vals) throws SQLException {
    if (field.getMappedBy() != null && !_orderInsert && !_orderUpdate) return;
    Collection coll = toCollection(vals);
    if (coll == null || coll.isEmpty()) return;

    ClassMapping rel = field.getElementMapping().getTypeMapping();
    int idx = 0;
    for (Iterator itr = coll.iterator(); itr.hasNext(); idx++)
      updateInverse(sm.getContext(), itr.next(), rel, rm, sm, idx);
  }
  /** This method updates the inverse columns of a 1-M related object with the given oid. */
  private void updateInverse(
      StoreContext ctx,
      Object inverse,
      ClassMapping rel,
      RowManager rm,
      OpenJPAStateManager sm,
      int idx)
      throws SQLException {
    OpenJPAStateManager invsm = RelationStrategies.getStateManager(inverse, ctx);
    if (invsm == null) return;

    ValueMapping elem = field.getElementMapping();
    ForeignKey fk = elem.getForeignKey();
    ColumnIO io = elem.getColumnIO();
    Column order = field.getOrderColumn();

    int action;
    boolean writeable;
    boolean orderWriteable;
    if (invsm.isNew() && !invsm.isFlushed()) {
      // no need to null inverse columns of new instance
      if (sm == null || sm.isDeleted()) return;
      writeable = io.isAnyInsertable(fk, false);
      orderWriteable = _orderInsert;
      action = Row.ACTION_INSERT;
    } else if (invsm.isDeleted()) {
      // no need to null inverse columns of deleted instance
      if (invsm.isFlushed() || sm == null || !sm.isDeleted()) return;
      writeable = true;
      orderWriteable = false;
      action = Row.ACTION_DELETE;
    } else {
      if (sm != null && sm.isDeleted()) sm = null;
      writeable = io.isAnyUpdatable(fk, sm == null);
      orderWriteable = field.getOrderColumnIO().isUpdatable(order, sm == null);
      action = Row.ACTION_UPDATE;
    }
    if (!writeable && !orderWriteable) return;

    assertInversable();

    // if this is an update, this might be the only mod to the row, so
    // make sure the where condition is set
    Row row = rm.getRow(fk.getTable(), action, invsm, true);
    if (action == Row.ACTION_UPDATE) row.wherePrimaryKey(invsm);

    // update the inverse pointer with our oid value
    if (writeable) row.setForeignKey(fk, io, sm);
    if (orderWriteable) row.setInt(order, idx);
  }
Ejemplo n.º 4
0
  public Row getRow(Table table, int action, OpenJPAStateManager sm, boolean create) {
    if (sm == null) return null;

    // check if request matches cached version
    if (_key != null
        && _key.table == table
        && _key.sm == sm
        && _row != null
        && _row.getAction() == action) return _row;

    Map<Key, PrimaryRow> map;
    if (action == Row.ACTION_DELETE) {
      if (_deletes == null && create) _deletes = new LinkedHashMap<Key, PrimaryRow>();
      map = _deletes;
    } else if (action == Row.ACTION_INSERT) {
      if (_inserts == null && create) _inserts = new LinkedHashMap<Key, PrimaryRow>();
      map = _inserts;
    } else {
      if (_updates == null && create) _updates = new LinkedHashMap<Key, PrimaryRow>();
      map = _updates;
    }
    if (map == null) return null;

    _key = new Key(table, sm);
    _row = map.get(_key);

    if (_row == null && create) {
      _row = new PrimaryRow(table, action, sm);
      map.put(_key, _row);
      if (_primaryOrder != null) {
        _row.setIndex(_primaryOrder.size());
        _primaryOrder.add(_row);
      }

      if (!_auto && action == Row.ACTION_INSERT) _auto = table.getAutoAssignedColumns().length > 0;
    }

    if (_row != null) _row.setFailedObject(sm.getManagedInstance());
    return _row;
  }
  public void update(OpenJPAStateManager sm, JDBCStore store, RowManager rm) throws SQLException {
    if (field.getMappedBy() != null && !_orderInsert && !_orderUpdate) return;

    Object obj = sm.fetchObject(field.getIndex());
    ChangeTracker ct = null;
    if (obj instanceof Proxy) {
      Proxy proxy = (Proxy) obj;
      if (Proxies.isOwner(proxy, sm, field.getIndex())) ct = proxy.getChangeTracker();
    }
    Column order = field.getOrderColumn();

    // if no fine-grained change tracking then just delete and reinsert
    // if no fine-grained change tracking or if an item was removed
    // from an ordered collection, delete and reinsert
    if (ct == null || !ct.isTracking() || (order != null && !ct.getRemoved().isEmpty())) {
      delete(sm, store, rm);
      insert(sm, rm, obj);
      return;
    }

    // null inverse columns for deletes and update them with our oid for
    // inserts
    ClassMapping rel = field.getElementMapping().getTypeMapping();
    StoreContext ctx = store.getContext();
    if (field.getMappedBy() == null) {
      Collection rem = ct.getRemoved();
      for (Iterator itr = rem.iterator(); itr.hasNext(); )
        updateInverse(ctx, itr.next(), rel, rm, null, 0);
    }

    Collection add = ct.getAdded();
    int seq = ct.getNextSequence();
    for (Iterator itr = add.iterator(); itr.hasNext(); seq++)
      updateInverse(ctx, itr.next(), rel, rm, sm, seq);
    if (order != null) ct.setNextSequence(seq);
  }
Ejemplo n.º 6
0
 public int hashCode() {
   return ((table == null) ? 0 : table.hashCode())
       + ((sm == null) ? 0 : sm.hashCode()) % Integer.MAX_VALUE;
 }
 public void insert(OpenJPAStateManager sm, JDBCStore store, RowManager rm) throws SQLException {
   if (field.getMappedBy() == null || _orderInsert || _orderUpdate)
     insert(sm, rm, sm.fetchObject(field.getIndex()));
 }