@Override public void flush() { // deferred backend operations not (yet!) implemented // persist updates to entities in the persistence context for (final Map.Entry<Key<?, ?>, Object> entry : persistenceContext.entrySet()) { // type safety warning should go away when we have a real PersistenceContext implementation updateInBackend((Key<Object, ?>) entry.getKey(), entry.getValue()); } }
/** * Returns true if this persistence context contains an entity retrievable by * the given key. The type matching is done in the same way as described in * {@link #get(Key)}. * * @param key * the identity of the entity to look for. Must not be null. * @return True if and only if this persistence context contains an entity * retrievable by the given key. */ public boolean contains(Key<?, ?> key) { return get(key) != null; }
@Override public void clear() { removedEntities.clear(); final List<?> entities = new ArrayList<>(persistenceContext.values()); for (final Object entity : entities) { detach(entity); } }
/** * Retrieves the entity instance identified by the given Key. * * @param key The key to look up. Must not be null. * @param properties JPA hints (standard and Errai-specific) for the lookup. * @return the entity instance, or null if the entity cannot be found. */ public <X> X find(final Key<X, ?> key, final Map<String, Object> properties) { X entity = cast(key.getEntityType().getJavaType(), persistenceContext.get(key)); if (entity == null) { entity = backend.get(key); if (entity != null) { persistenceContext.put(key, entity); ((ErraiIdentifiableType<X>) key.getEntityType()).deliverPostLoad(entity); } } return entity; }
persistenceContext.put(key, entity); backend.put(key, entity); entityType.deliverPostPersist(entity); persistenceContext.put(key, mergeTarget); backend.put(key, mergeTarget); persistenceContext.remove(key); break; case REMOVED: case MANAGED: entityType.deliverPreRemove(entity); persistenceContext.remove(key); removedEntities.put(key, entity); backend.remove(key);
/** * Constructor for building custom-purpose EntityManager instances. For common * usecases, simply use {@code @Inject EntityManager em} and let the * {@link ErraiEntityManagerProducer} handle the prerequisites for you. */ public ErraiEntityManager( final ErraiMetamodel metamodel, final Map<String, TypedQueryFactory> namedQueries, final StorageBackendFactory storageBackendFactory) { this.metamodel = Assert.notNull(metamodel); this.namedQueries = Assert.notNull(namedQueries); this.persistenceContext = new PersistenceContext(metamodel); this.logger = LoggerFactory.getLogger(ErraiEntityManager.class); // Caution: we're handing out a reference to this partially constructed instance! this.backend = storageBackendFactory.createInstanceFor(this); }
@Override public boolean contains(final Object entity) { final Object found = persistenceContext.get(keyFor(entity)); return found == entity; }
Object inPersistenceContext = persistenceContext.get(key); while (inPersistenceContext instanceof WrappedPortable) { inPersistenceContext = ((WrappedPortable) inPersistenceContext).unwrap();