/** * 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>. * * @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 ErraiIdentifiableType<X> entityType, final X entity) { ErraiSingularAttribute<? super X, ?> idAttr; switch (entityType.getIdType().getPersistenceType()) { case BASIC: idAttr = entityType.getId(entityType.getIdType().getJavaType()); break; default: throw new RuntimeException(entityType.getIdType().getPersistenceType() + " ids are not yet supported"); } Object id = idAttr.get(entity); if ( idAttr.isGeneratedValue() && (id == null || (id instanceof Number && ((Number) id).doubleValue() == 0.0)) ) { id = generateAndSetLocalId(entity, idAttr); // TODO track this generated ID for later reconciliation with the server } return new Key<>(entityType, id); }
/** * 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); }
Object idToReference = attrEntityType.getId(Object.class).get(entityToReference); JSONValue ref; if (idToReference == null) {
ErraiSingularAttribute<? super E, Object> idAttr = entityType.getId(Object.class); changeId(entityType, icr.getOldId(), idAttr.get(newEntity)); desiredStateEm.merge(newEntity); if (inTheWay != null) { ErraiIdentifiableType<E> entityType = desiredStateEm.getMetamodel().entity(entityClass); ErraiSingularAttribute<? super E, Object> idAttr = entityType.getId(Object.class); ErraiIdGenerator<Object> idGenerator = idAttr.getValueGenerator(); if (idGenerator != null && idGenerator.hasNext(desiredStateEm)) {
final ErraiIdentifiableType<X> entityType = getMetamodel().entity(getNarrowedClass(entity)); if (backend.isModified(key, entity)) { final Object currentId = entityType.getId(Object.class).get(entity); if (!key.getId().equals(currentId)) { throw new PersistenceException(
/** * Returns a Key instance based on the given JSON object. * * @param em * The entity manager that can be used to look up the entity type * corresponding with the key. * @param key * The properties of the key to create. * @param failIfNotFound * If true, and the entity type given in {@code key} is not known to * {@code em}, an IllegalArgumentException will be thrown. * @return An instance of Key that corresponds with the entity type and ID of * the given JSON object. */ public static Key<?, ?> fromJsonObject(ErraiEntityManager em, JSONObject key, boolean failIfNotFound) { String entityClassName = key.get("entityType").isString().stringValue(); ErraiIdentifiableType<Object> et = em.getMetamodel().entity(entityClassName, failIfNotFound); if (et == null) { return null; } ErraiSingularAttribute<?, Object> idAttr = et.getId(Object.class); Object id = JsonUtil.basicValueFromJson(key.get("id"), idAttr.getJavaType()); return new Key<Object, Object>(et, id); } }
int index = 0; for (E element : (Iterable<E>) attrValue) { Object idToReference = attrEntityType.getId(Object.class).get(element); JSONValue ref; if (idToReference == null) {