Ejemplo n.º 1
0
  // copied and adapted from OJB's MtoNBroker
  protected void updateMtoNRelation(PersistenceBroker pb, RelationTupleInfo tupleInfo) {
    DomainObject obj1 = tupleInfo.obj1;
    DomainObject obj2 = tupleInfo.obj2;

    ClassDescriptor cld1 = pb.getDescriptorRepository().getDescriptorFor(obj1.getClass());
    CollectionDescriptor cod = cld1.getCollectionDescriptorByName(tupleInfo.colNameOnObj1);
    if (cod == null) {
      // try the mapping on the other object
      cld1 = pb.getDescriptorRepository().getDescriptorFor(obj2.getClass());
      cod = cld1.getCollectionDescriptorByName(tupleInfo.colNameOnObj2);

      // switch objects
      obj1 = tupleInfo.obj2;
      obj2 = tupleInfo.obj1;
    }

    String[] oidColumns = new String[2];
    oidColumns[0] = cod.getFksToThisClass()[0];
    oidColumns[1] = cod.getFksToItemClass()[0];

    ValueContainer[] oidValues = new ValueContainer[2];
    oidValues[0] = new ValueContainer(obj1.getOid(), OID_JDBC_TYPE);
    oidValues[1] = new ValueContainer(obj2.getOid(), OID_JDBC_TYPE);

    String table = cod.getIndirectionTable();

    // always remove the tuple
    String sqlStmt = pb.serviceSqlGenerator().getDeleteMNStatement(table, oidColumns, null);
    pb.serviceJdbcAccess().executeUpdateSQL(sqlStmt, cld1, oidValues, null);

    // if it was not to remove but to add, then add it
    // this "delete-first, add-after" serves to ensure that we can add
    // multiple times
    // the same tuple to a relation and still have the Set semantics for the
    // relation.
    if (!tupleInfo.remove) {
      sqlStmt = pb.serviceSqlGenerator().getInsertMNStatement(table, oidColumns, EMPTY_ARRAY);
      pb.serviceJdbcAccess().executeUpdateSQL(sqlStmt, cld1, oidValues, null);
    }
  }