/** * Determines whether the instance is <em>New</em>, i.e. just created and not stored in database yet. * * @param entity entity instance * @return - true if the instance is a new persistent entity, or if it is actually in Managed state * but newly-persisted in this transaction <br> * - true if the instance is a new non-persistent entity never returned from DataManager <br> * - false otherwise * @throws IllegalArgumentException if entity instance is null */ public boolean isNew(Object entity) { checkNotNullArgument(entity, "entity is null"); if (entity instanceof BaseGenericIdEntity) { return BaseEntityInternalAccess.isNew((BaseGenericIdEntity) entity); } else if (entity instanceof AbstractNotPersistentEntity) { return BaseEntityInternalAccess.isNew((AbstractNotPersistentEntity) entity); } else { if (log.isTraceEnabled()) { log.trace("EntityStates.isNew is called for unsupported type '{}'. Stacktrace:\n{}", entity.getClass().getSimpleName(), StackTrace.asString()); } } return false; }
@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 + "]"; } }
private void detachAll() { Collection<Entity> instances = container.getAllInstances(); for (Object instance : instances) { if (instance instanceof BaseGenericIdEntity && BaseEntityInternalAccess.isNew((BaseGenericIdEntity) instance)) { container.getNewDetachedInstances().add((Entity) instance); } } javax.persistence.EntityManager jpaEm = persistence.getEntityManager(container.getStoreName()).getDelegate(); jpaEm.flush(); jpaEm.clear(); for (Object instance : instances) { makeDetached(instance); } }
EntityChangedEvent.Type type = null; AttributeChanges attributeChanges = null; if (info.onCreated && BaseEntityInternalAccess.isNew((BaseGenericIdEntity) entity)) { type = EntityChangedEvent.Type.CREATED; attributeChanges = getEntityAttributeChanges(entity, false);
public void detach(EntityManager entityManager, Entity entity) { UnitOfWork unitOfWork = entityManager.getDelegate().unwrap(UnitOfWork.class); String storeName = getStorageName(unitOfWork); if (entity instanceof BaseGenericIdEntity) { fireBeforeDetachEntityListener((BaseGenericIdEntity) entity, storeName); ContainerResourceHolder container = getInstanceContainerResourceHolder(storeName); container.unregisterInstance(entity, unitOfWork); if (BaseEntityInternalAccess.isNew((BaseGenericIdEntity) entity)) { container.getNewDetachedInstances().add(entity); } } makeDetached(entity); }
@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); } }
protected void mergeSystemState(Entity srcEntity, Entity dstEntity) { if (dstEntity instanceof BaseGenericIdEntity) { BaseEntityInternalAccess.copySystemState((BaseGenericIdEntity) srcEntity, (BaseGenericIdEntity) dstEntity); if (srcEntity instanceof FetchGroupTracker && dstEntity instanceof FetchGroupTracker) { FetchGroup srcFetchGroup = ((FetchGroupTracker) srcEntity)._persistence_getFetchGroup(); FetchGroup dstFetchGroup = ((FetchGroupTracker) dstEntity)._persistence_getFetchGroup(); if (srcFetchGroup == null || dstFetchGroup == null) { ((FetchGroupTracker) dstEntity)._persistence_setFetchGroup(null); } else { ((FetchGroupTracker) dstEntity)._persistence_setFetchGroup(FetchGroupUtils.mergeFetchGroups(srcFetchGroup, dstFetchGroup)); } } } else if (dstEntity instanceof AbstractNotPersistentEntity) { BaseEntityInternalAccess.setNew((AbstractNotPersistentEntity) dstEntity, BaseEntityInternalAccess.isNew((AbstractNotPersistentEntity) srcEntity)); } }
@Override protected boolean isLoadedSpecificCheck(Object entity, String property, MetaClass metaClass, MetaProperty metaProperty) { if (metadataTools.isEmbeddable(metaClass) || (entity instanceof BaseGenericIdEntity && BaseEntityInternalAccess.isNew((BaseGenericIdEntity) entity))) { //TODO eude: // this is workaround for unexpected EclipseLink behaviour when PersistenceUnitUtil.isLoaded // throws exception if embedded entity refers to persistent entity return checkIsLoadedWithGetter(entity, property); } if (!metadataTools.isPersistent(metaClass)) { return checkIsLoadedWithGetter(entity, property); } EntityManagerFactory jpaEmf = ((PersistenceImpl) persistence).getJpaEmf(metadataTools.getStoreName(metaClass)); return jpaEmf.getPersistenceUnitUtil().isLoaded(entity, property); } }
@SuppressWarnings("unchecked") protected void copySystemState(Entity srcEntity, Entity dstEntity) { if (dstEntity instanceof BaseGenericIdEntity) { ((BaseGenericIdEntity) dstEntity).setId(srcEntity.getId()); BaseEntityInternalAccess.copySystemState((BaseGenericIdEntity) srcEntity, (BaseGenericIdEntity) dstEntity); if (srcEntity instanceof FetchGroupTracker && dstEntity instanceof FetchGroupTracker) { FetchGroup srcFetchGroup = ((FetchGroupTracker) srcEntity)._persistence_getFetchGroup(); ((FetchGroupTracker) dstEntity)._persistence_setFetchGroup(srcFetchGroup); } } else if (dstEntity instanceof AbstractNotPersistentEntity) { ((AbstractNotPersistentEntity) dstEntity).setId((UUID) srcEntity.getId()); BaseEntityInternalAccess.setNew((AbstractNotPersistentEntity) dstEntity, BaseEntityInternalAccess.isNew((AbstractNotPersistentEntity) srcEntity)); } if (dstEntity instanceof Versioned) { ((Versioned) dstEntity).setVersion(((Versioned) srcEntity).getVersion()); } }
if (BaseEntityInternalAccess.isNew((BaseGenericIdEntity) entity)) { typeNames.add(entity.getMetaClass().getName());
Map<String, CategoryAttributeValue> dynamicAttributes = ((BaseGenericIdEntity) entity).getDynamicAttributes(); for (CategoryAttributeValue cav : dynamicAttributes.values()) { if (BaseEntityInternalAccess.isNew(cav)) { changes.add(new AttributeChanges.Change(DynamicAttributesUtils.encodeAttributeCode(cav.getCode()), null)); } else {
BaseEntityInternalAccess.setNew(destGenericEntity, BaseEntityInternalAccess.isNew(sourceGenericEntity)); destGenericEntity.setDynamicAttributes(sourceGenericEntity.getDynamicAttributes());