@Override public boolean contains(final Object entity) { final Object found = persistenceContext.get(keyFor(entity)); return found == entity; }
/** * Creates the key that describes the given entity, <b>generating and setting * it if it is presently unset and the given entity type's ID is configured to * be generated on demand</b>. This version of the {@code keyFor()} method * assumes the given object's entity type can be obtained by calling {@code * entity.getClass()}. If you already have a specific entity type in mind, use * the {@link #keyFor(ErraiIdentifiableType, Object)} version of the method. * * @param entityType * The entity type of the entity * @param entity * The entity instance. <b>Side effect: this instance may have its ID * value initialized as a result of this call</b>. * @return The key for the given entity, which--for generated values--may have * just been set on the entity. */ public <X> Key<X, ?> keyFor(final X entity) { final ErraiIdentifiableType<X> entityType = getMetamodel().entity(getNarrowedClass(entity)); return keyFor(entityType, entity); }
private static <X, Y> void copySingularAssociation( ErraiEntityManager em, ErraiAttribute<X, Y> attr, X targetEntity, X sourceEntity) { ErraiIdentifiableType<Y> relatedEntityType = em.getMetamodel().entity(attr.getJavaType()); Y oldRelatedEntity = attr.get(sourceEntity); Y resolvedEntity; if (oldRelatedEntity == null) { resolvedEntity = null; } else { Key<Y, ?> key = em.keyFor(oldRelatedEntity); resolvedEntity = em.find(key, Collections.<String,Object>emptyMap()); if (resolvedEntity == null) { resolvedEntity = relatedEntityType.newInstance(); } } attr.set(targetEntity, resolvedEntity); }
private static <X, C, E> void copyPluralAssociation( ErraiEntityManager em, ErraiPluralAttribute<X, C, E> attr, X targetEntity, X sourceEntity) { C oldCollection = attr.get(sourceEntity); C newCollection; if (oldCollection == null) { newCollection = null; } else { newCollection = attr.createEmptyCollection(); ErraiIdentifiableType<E> elemType = em.getMetamodel().entity(attr.getElementType().getJavaType()); // TODO support map-valued plural attributes for (Object oldEntry : (Collection<?>) oldCollection) { Key<Object, ?> key = em.keyFor(oldEntry); Object resolvedEntry = em.find(key, Collections.<String,Object>emptyMap()); if (resolvedEntry == null) { resolvedEntry = elemType.newInstance(); } ((Collection) newCollection).add(resolvedEntry); } } attr.set(targetEntity, newCollection); }
E resolved = expectedStateEm.find(expectedStateEm.keyFor(requestedNew), Collections.<String,Object>emptyMap()); expectedStateEm.remove(resolved); resolved = desiredStateEm.find(desiredStateEm.keyFor(requestedNew), Collections.<String,Object>emptyMap()); desiredStateEm.remove(resolved); DeleteResponse<E> dr = (DeleteResponse<E>) response; System.out.println(" -> Delete " + dr.getEntity()); E resolved = expectedStateEm.find(expectedStateEm.keyFor(dr.getEntity()), Collections.<String,Object>emptyMap()); expectedStateEm.remove(resolved); expectedStateEm.detach(resolved); resolved = desiredStateEm.find(desiredStateEm.keyFor(dr.getEntity()), Collections.<String,Object>emptyMap()); if (resolved != null) { desiredStateEm.remove(resolved); Class<E> entityClass = (Class<E>) nrer.getEntity().getClass(); Key<E, ?> conflictingKey = desiredStateEm.keyFor(nrer.getEntity()); E inTheWay = desiredStateEm.find(conflictingKey, Collections.<String,Object>emptyMap()); if (inTheWay != null) {
final ErraiIdentifiableType<X> entityType = getMetamodel().entity(getNarrowedClass(entity)); final Key<X, ?> key = keyFor(entityType, entity); final EntityState oldState = getState(key, entity);