private void writeObject(java.io.ObjectOutputStream out) throws IOException { if (isManaged(this)) { setManaged(this, false); setDetached(this, true); } out.defaultWriteObject(); }
/** * Determines whether the instance is <em>Managed</em>, i.e. attached to a persistence context. * * @param entity entity instance * @return - true if the instance is managed,<br> * - false if it is New (and not yet persisted) or Detached, or if it is not a persistent entity * @throws IllegalArgumentException if entity instance is null */ public boolean isManaged(Object entity) { checkNotNullArgument(entity, "entity is null"); if (entity instanceof BaseGenericIdEntity) { return BaseEntityInternalAccess.isManaged((BaseGenericIdEntity) entity); } else { if (log.isTraceEnabled()) { log.trace("EntityStates.isManaged is called for unsupported type '{}'. Stacktrace:\n{}", entity.getClass().getSimpleName(), StackTrace.asString()); } } return false; }
@Override @SuppressWarnings("finally") public void serialize(Object object, OutputStream os) { try (Output output = new CubaOutput(os)) { if (object instanceof BaseGenericIdEntity && BaseEntityInternalAccess.isManaged((BaseGenericIdEntity) object)) { BaseEntityInternalAccess.setDetached((BaseGenericIdEntity) object, true); } kryos.get().writeClassAndObject(output, object); } }
@Override public String toString() { String state = ""; if (isNew(this)) state += "new,"; if (isManaged(this)) state += "managed,"; if (isDetached(this)) state += "detached,"; if (isRemoved(this)) state += "removed,"; if (state.length() > 0) state = state.substring(0, state.length() - 1); return getClass().getName() + "-" + getId() + " [" + state + "]"; } }
/** * Makes a newly constructed object detached. The detached object can be passed to {@code DataManager.commit()} or * to {@code EntityManager.merge()} to save its state to the database. * <p>If an object with such ID does not exist in the database, a new object will be inserted. * <p>If the entity is {@code Versioned}, the version attribute should be equal to the latest version existing in * the database, or null for a new object. * * @param entity entity in the New state * @throws IllegalStateException if the entity is Managed * @see #isDetached(Object) * @see #makePatch(BaseGenericIdEntity) */ public void makeDetached(BaseGenericIdEntity entity) { checkNotNullArgument(entity, "entity is null"); if (BaseEntityInternalAccess.isManaged(entity)) throw new IllegalStateException("entity is managed"); BaseEntityInternalAccess.setNew(entity, false); BaseEntityInternalAccess.setDetached(entity, true); }
/** * Makes a newly constructed object a patch object. The patch object is {@code !isNew() && !isDetached() && !isManaged()}. * The patch object can be passed to {@code DataManager.commit()} or * to {@code EntityManager.merge()} to save its state to the database. Only <b>non-null values</b> of attributes are * updated. * <p>If an object with such ID does not exist in the database, a new object will be inserted. * <p>If the entity is {@code Versioned}, the version attribute should be null or equal to the latest version existing in * the database. * * @param entity entity in the New or Detached state * @throws IllegalStateException if the entity is Managed * @see #isDetached(Object) * @see #makeDetached(BaseGenericIdEntity) */ public void makePatch(BaseGenericIdEntity entity) { checkNotNullArgument(entity, "entity is null"); if (BaseEntityInternalAccess.isManaged(entity)) throw new IllegalStateException("entity is managed"); BaseEntityInternalAccess.setNew(entity, false); BaseEntityInternalAccess.setDetached(entity, false); } }