protected <Y> void parseSingularJsonReference( X targetEntity, ErraiSingularAttribute<? super X, Y> attr, JSONValue attrJsonValue, ErraiEntityManager eem) { if (attrJsonValue == null || attrJsonValue.isNull() != null) return; Key<Y, ?> key = (Key<Y, ?>) Key.fromJsonObject(eem, attrJsonValue.isObject(), true); logger.trace(" looking for " + key); Y value = eem.find(key, Collections.<String,Object>emptyMap()); attr.set(targetEntity, value); }
/** * Generates a new ID value for the given entity instance that is guaranteed * to be unique <i>on this client</i>. If the entity instance with this ID is * ever synchronized to the server, this client-local ID will be replaced by a * permanent server-generated ID. * <p> * This method only works for attributes that are configured as * {@code @GeneratedValue}s. The GenerationType has no effect locally, but of * course it will come into play on the server side when and if the entity is * synchronized to the server. * * @param entityInstance * the entity instance to receive the generated ID. This attribute of * that entity instance will be set to the newly generated ID value. * @return the generated ID value, which has already been set on the entity * instance. */ private <X, T> T generateAndSetLocalId(final X entityInstance, final ErraiSingularAttribute<X, T> attr) { final T nextId = attr.getValueGenerator().next(this); attr.set(entityInstance, nextId); return nextId; }
/** * Changes the ID of an existing entity in desiredStateEm. * * @param entityType The metamodel type for the entity whose ID is to be changed * @param oldId The ID that the entity currently has. * @param newId The ID that the entity will have when this method returns. */ private <E> void changeId(ErraiIdentifiableType<E> entityType, Object oldId, Object newId) { // XXX this routine is probably better handled internally by the ErraiEntityManager // TODO what about related entities that refer to this one? (needs tests) E entity = desiredStateEm.find(entityType.getJavaType(), oldId); desiredStateEm.remove(entity); desiredStateEm.flush(); desiredStateEm.detach(entity); entityType.getId(Object.class).set(entity, newId); desiredStateEm.persist(entity); }