private void writeObject(java.io.ObjectOutputStream out) throws IOException { if (isManaged(this)) { setManaged(this, false); setDetached(this, true); } out.defaultWriteObject(); }
@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); } }
/** * 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); } }
protected void makeDetached(Object instance) { if (instance instanceof BaseGenericIdEntity) { BaseEntityInternalAccess.setNew((BaseGenericIdEntity) instance, false); BaseEntityInternalAccess.setManaged((BaseGenericIdEntity) instance, false); BaseEntityInternalAccess.setDetached((BaseGenericIdEntity) instance, true); } if (instance instanceof FetchGroupTracker) { ((FetchGroupTracker) instance)._persistence_setSession(null); } if (instance instanceof ChangeTracker) { ((ChangeTracker) instance)._persistence_setPropertyChangeListener(null); } }
public void registerInstance(Entity entity, EntityManager entityManager) { if (!TransactionSynchronizationManager.isActualTransactionActive()) throw new RuntimeException("No transaction"); UnitOfWork unitOfWork = entityManager.getDelegate().unwrap(UnitOfWork.class); getInstanceContainerResourceHolder(getStorageName(unitOfWork)).registerInstanceForUnitOfWork(entity, unitOfWork); if (entity instanceof BaseGenericIdEntity) { BaseEntityInternalAccess.setDetached((BaseGenericIdEntity) entity, false); } }
@Override public void afterCompletion(int status) { try { Collection<Entity> instances = container.getAllInstances(); if (log.isTraceEnabled()) log.trace("ContainerResourceSynchronization.afterCompletion: instances = " + instances); for (Object instance : instances) { if (instance instanceof BaseGenericIdEntity) { if (status == TransactionSynchronization.STATUS_COMMITTED) { if (BaseEntityInternalAccess.isNew((BaseGenericIdEntity) instance)) { // new instances become not new and detached only if the transaction was committed BaseEntityInternalAccess.setNew((BaseGenericIdEntity) instance, false); } } else { // commit failed or the transaction was rolled back makeDetached(instance); for (Entity entity : container.getNewDetachedInstances()) { BaseEntityInternalAccess.setNew((BaseGenericIdEntity) entity, true); BaseEntityInternalAccess.setDetached((BaseGenericIdEntity) entity, false); } } } } for (AfterCompleteTransactionListener listener : afterCompleteTxListeners) { listener.afterComplete(status == TransactionSynchronization.STATUS_COMMITTED, instances); } } finally { super.afterCompletion(status); } }
BaseGenericIdEntity<?> sourceGenericEntity = (BaseGenericIdEntity<?>) source; BaseEntityInternalAccess.setDetached(destGenericEntity, BaseEntityInternalAccess.isDetached(sourceGenericEntity)); BaseEntityInternalAccess.setNew(destGenericEntity, BaseEntityInternalAccess.isNew(sourceGenericEntity)); destGenericEntity.setDynamicAttributes(sourceGenericEntity.getDynamicAttributes());