/** * Clears all the mapping between this obj entity and its current db entity. Clears mapping * between entities, attributes and relationships. */ public void clearDbMapping() { if (dbEntityName == null) { return; } for (ObjAttribute attribute : getAttributeMap().values()) { DbAttribute dbAttr = attribute.getDbAttribute(); if (dbAttr != null) { attribute.setDbAttributePath(null); } } for (ObjRelationship relationship : getRelationships()) { relationship.clearDbRelationships(); } dbEntityName = null; }
/** * Returns a named attribute that is either declared in this ObjEntity or is inherited. In any * case returned attribute 'getEntity' method will return this entity. Returns null if no matching * attribute is found. */ @Override public ObjAttribute getAttribute(String name) { ObjAttribute attribute = (ObjAttribute) super.getAttribute(name); if (attribute != null) { return attribute; } // check embedded attribute int dot = name.indexOf('.'); if (dot > 0 && dot < name.length() - 1) { ObjAttribute embedded = getAttribute(name.substring(0, dot)); if (embedded instanceof EmbeddedAttribute) { return ((EmbeddedAttribute) embedded).getAttribute(name.substring(dot + 1)); } } // check super attribute ObjEntity superEntity = getSuperEntity(); if (superEntity != null) { ObjAttribute superAttribute = superEntity.getAttribute(name); if (superAttribute == null) { return null; } // decorate returned attribute to make it appear as if it belongs to // this // entity ObjAttribute decoratedAttribute = new ObjAttribute(superAttribute); decoratedAttribute.setEntity(this); String pathOverride = attributeOverrides.get(name); if (pathOverride != null) { decoratedAttribute.setDbAttributePath(pathOverride); } return decoratedAttribute; } return null; }
/** Recursively appends all attributes in the entity inheritance hierarchy. */ final void appendAttributes(Map<String, ObjAttribute> map) { map.putAll((Map<String, ObjAttribute>) super.getAttributeMap()); ObjEntity superEntity = getSuperEntity(); if (superEntity != null) { SortedMap<String, ObjAttribute> attributeMap = new TreeMap<String, ObjAttribute>(); superEntity.appendAttributes(attributeMap); for (String attributeName : attributeMap.keySet()) { String overridedDbPath = attributeOverrides.get(attributeName); ObjAttribute attribute = new ObjAttribute(attributeMap.get(attributeName)); attribute.setEntity(this); if (overridedDbPath != null) { attribute.setDbAttributePath(overridedDbPath); } map.put(attributeName, attribute); } } }
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; }