/** * Returns mapped ObjEntity for this object. If an object is transient or is not * mapped returns null. * * @since 1.2 */ public ObjEntity getObjEntity() { return (getObjectContext() != null) ? getObjectContext() .getEntityResolver() .lookupObjEntity(this) : null; }
/** * Called before establishing a relationship with another object. Applies * "persistence by reachability" logic, pulling one of the two objects to a * DataConext of another object in case one of the objects is transient. If * both objects are persistent, and they don't have the same DataContext, * CayenneRuntimeException is thrown. * * @since 1.2 */ protected void willConnect(String relationshipName, Persistent object) { // first handle most common case - both objects are in the same // ObjectContext or target is null if (object == null || this.getObjectContext() == object.getObjectContext()) { return; } else if (this.getObjectContext() == null && object.getObjectContext() != null) { object.getObjectContext().registerNewObject(this); } else if (this.getObjectContext() != null && object.getObjectContext() == null) { this.getObjectContext().registerNewObject(object); } else { throw new CayenneRuntimeException("Cannot set object as destination of relationship %s" + " because it is in a different ObjectContext", relationshipName); } }
public void removeToManyTarget(String relName, DataObject value, boolean setReverse) { // Now do the rest of the normal handling (regardless of whether it was // flattened or not) Object holder = readProperty(relName); // call 'propertyChanged' AFTER readProperty as readProperty ensures // that this // object fault is resolved getObjectContext().propertyChanged(this, relName, value, null); // TODO: andrus 8/20/2007 - can we optimize this somehow, avoiding type // checking?? if (holder instanceof Collection) { ((Collection<Object>) holder).remove(value); } else if (holder instanceof Map) { ((Map<Object, Object>) holder).remove(getMapKey(relName, value)); } if (value != null && setReverse) { unsetReverseRelationship(relName, value); } }
public void addToManyTarget(String relName, DataObject value, boolean setReverse) { if (value == null) { throw new NullPointerException("Attempt to add null target DataObject."); } willConnect(relName, value); // Now do the rest of the normal handling (regardless of whether it was // flattened or not) Object holder = readProperty(relName); // call 'propertyChanged' AFTER readProperty as readProperty ensures // that this // object fault is resolved getObjectContext().propertyChanged(this, relName, null, value); // TODO: andrus 8/20/2007 - can we optimize this somehow, avoiding type // checking?? if (holder instanceof Collection) { ((Collection<Object>) holder).add(value); } else if (holder instanceof Map) { ((Map<Object, Object>) holder).put(getMapKey(relName, value), value); } if (setReverse) { setReverseRelationship(relName, value); } }
/** * Called before establishing a relationship with another object. Applies "persistence * by reachability" logic, pulling one of the two objects to a DataConext of another * object in case one of the objects is transient. If both objects are persistent, and * they don't have the same DataContext, CayenneRuntimeException is thrown. * * @since 1.2 */ protected void willConnect(String relationshipName, DataObject dataObject) { // first handle most common case - both objects are in the same // DataContext or target is null if (dataObject == null || this.getObjectContext() == dataObject.getObjectContext()) { return; } else if (this.getObjectContext() == null && dataObject.getObjectContext() != null) { dataObject.getDataContext().registerNewObject(this); } else if (this.getObjectContext() != null && dataObject.getObjectContext() == null) { this.getDataContext().registerNewObject(dataObject); } else { throw new CayenneRuntimeException( "Cannot set object as destination of relationship " + relationshipName + " because it is in a different DataContext"); } }
public void setToOneTarget(String relationshipName, DataObject value, boolean setReverse) { willConnect(relationshipName, value); Object oldTarget = readProperty(relationshipName); if (oldTarget == value) { return; } getObjectContext().propertyChanged(this, relationshipName, oldTarget, value); if (setReverse) { // unset old reverse relationship if (oldTarget instanceof DataObject) { unsetReverseRelationship(relationshipName, (DataObject) oldTarget); } // set new reverse relationship if (value != null) { setReverseRelationship(relationshipName, value); } } objectContext.prepareForAccess(this, relationshipName, false); writePropertyDirectly(relationshipName, value); }
ObjEntity objEntity = getObjectContext().getEntityResolver().getObjEntity(this); if (objEntity == null) { throw new CayenneRuntimeException("No ObjEntity mapping found for DataObject %s", getClass().getName());