Example #1
0
  /**
   * Returns the names of DbAtributes that comprise the primary key of the parent DbEntity.
   *
   * @since 3.0
   */
  public Collection<String> getPrimaryKeyNames() {
    DbEntity dbEntity = getDbEntity();

    // abstract entities may have no DbEntity mapping
    if (dbEntity == null) {
      return Collections.emptyList();
    }

    Collection<DbAttribute> pkAttributes = dbEntity.getPrimaryKeys();
    Collection<String> names = new ArrayList<String>(pkAttributes.size());

    for (DbAttribute pk : pkAttributes) {
      names.add(pk.getName());
    }

    return Collections.unmodifiableCollection(names);
  }
Example #2
0
  private Collection<ObjAttribute> getMutablePrimaryKeys() {
    if (getDbEntity() == null) {
      throw new CayenneRuntimeException("No DbEntity for ObjEntity: " + getName());
    }

    Collection<DbAttribute> pkAttributes = getDbEntity().getPrimaryKeys();
    Collection<ObjAttribute> attributes = new ArrayList<ObjAttribute>(pkAttributes.size());

    for (DbAttribute pk : pkAttributes) {
      ObjAttribute attribute = getAttributeForDbAttribute(pk);

      // create synthetic attribute
      if (attribute == null) {
        attribute =
            new SyntheticPKObjAttribute(NameConverter.underscoredToJava(pk.getName(), false));
        attribute.setDbAttributePath(pk.getName());
        attribute.setType(TypesMapping.getJavaBySqlType(pk.getType()));
      }

      attributes.add(attribute);
    }

    return attributes;
  }
Example #3
0
  /**
   * Returns ObjAttribute of this entity that maps to <code>dbAttribute</code> parameter. Returns
   * null if no such attribute is found.
   */
  public ObjAttribute getAttributeForDbAttribute(DbAttribute dbAttribute) {

    for (ObjAttribute next : getAttributeMap().values()) {

      if (next instanceof EmbeddedAttribute) {
        ObjAttribute embeddedAttribute =
            ((EmbeddedAttribute) next).getAttributeForDbPath(dbAttribute.getName());
        if (embeddedAttribute != null) {
          return embeddedAttribute;
        }
      } else {
        if (next.getDbAttribute() == dbAttribute) {
          return next;
        }
      }
    }

    return null;
  }