/** * If ObjEntity qualifier is set, asks it to inject initial value to an object. Also performs all * Persistent initialization operations */ protected void injectInitialValue(Object obj) { // must follow this exact order of property initialization per CAY-653, // i.e. have // the id and the context in place BEFORE setPersistence is called Persistent object = (Persistent) obj; object.setObjectContext(this); object.setPersistenceState(PersistenceState.NEW); GraphManager graphManager = getGraphManager(); synchronized (graphManager) { graphManager.registerNode(object.getObjectId(), object); graphManager.nodeCreated(object.getObjectId()); } ObjEntity entity; try { entity = getEntityResolver().getObjEntity(object.getClass()); } catch (CayenneRuntimeException ex) { // ObjEntity cannot be fetched, ignored entity = null; } if (entity != null) { if (entity.getDeclaredQualifier() instanceof ValueInjector) { ((ValueInjector) entity.getDeclaredQualifier()).injectValue(object); } } // invoke callbacks getEntityResolver().getCallbackRegistry().performCallbacks(LifecycleEvent.POST_ADD, object); }
/** @since 3.1 */ @Override public <T extends Persistent> T localObject(T objectFromAnotherContext) { if (objectFromAnotherContext == null) { throw new NullPointerException("Null object argument"); } ObjectId id = objectFromAnotherContext.getObjectId(); // first look for the ID in the local GraphManager synchronized (getGraphManager()) { T localObject = (T) getGraphManager().getNode(id); if (localObject != null) { return localObject; } // create a hollow object, optimistically assuming that the ID we // got from // 'objectFromAnotherContext' is a valid ID either in the parent // context or in // the DB. This essentially defers possible FaultFailureExceptions. ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(id.getEntityName()); Persistent persistent = (Persistent) descriptor.createObject(); persistent.setObjectContext(this); persistent.setObjectId(id); persistent.setPersistenceState(PersistenceState.HOLLOW); getGraphManager().registerNode(id, persistent); return (T) persistent; } }