if (!cascadeAcross.isAssociation()) return; final R sourceRelatedEntity = cascadeAcross.get(sourceEntity); logger.trace("*** Cascade " + cascadeType + " across " + cascadeAcross.getName() + " to " + sourceRelatedEntity + "?"); else if (cascadeAcross.cascades(cascadeType)) { logger.trace(" Yes"); if (cascadeAcross.isCollection()) { final R collectionOfMergeTargets = ((ErraiPluralAttribute<X, R, ?>) cascadeAcross).createEmptyCollection(); for (final Object element : (Iterable<?>) sourceRelatedEntity) { cascadeAcross.set(targetEntity, collectionOfMergeTargets); final R originalTargetRelatedEntity = cascadeAcross.get(targetEntity); if (resolvedTarget != originalTargetRelatedEntity) { cascadeAcross.set(targetEntity, resolvedTarget); final R resolvedTargetRelatedEntity = cascadeAcross.get(targetEntity); boolean relatedEntitiesAreManaged = true; if (cascadeAcross.isCollection()) { final Collection<?> children = (Collection<?>) resolvedTargetRelatedEntity; for (final Object child : children) { throw new IllegalStateException( "Entity " + targetEntity + " references an unsaved entity via relationship attribute [" + cascadeAcross.getName() + "]. Save related attribute before flushing or change" + " cascade rule to include " + cascadeType);
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); }
protected <Y> void parseInlineJson(X targetEntity, ErraiAttribute<? super X, Y> attr, JSONValue attrJsonValue, ErraiEntityManager eem) { Class<Y> attributeType = attr.getJavaType(); Y value; // FIXME this should search all managed types, or maybe all embeddables. not just entities. if (eem.getMetamodel().getEntities().contains(attributeType)) { ErraiIdentifiableType<Y> attrEntityType = eem.getMetamodel().entity(attributeType); value = attrEntityType.fromJson(eem, attrJsonValue); } else { value = JsonUtil.basicValueFromJson(attrJsonValue, attributeType); } attr.set(targetEntity, value); }
/** * Returns an inline JSON representation of the value of the given attribute * of the given entity instance. * * @param targetEntity * The instance of the entity to retrieve the attribute value from. * Not null. * @param attr * The attribute to read from {@code targetEntity}. Not null. * @param eem * The ErraiEntityManager that owns the entity. Not null. * @return a JSONValue that represents the requested attribute value of the * given entity. Never null, although it could be JSONNull. */ private <Y> JSONValue makeInlineJson(X targetEntity, ErraiAttribute<? super X, Y> attr, ErraiEntityManager eem) { Class<Y> attributeType = attr.getJavaType(); Y attrValue = attr.get(Assert.notNull(targetEntity)); // FIXME this should search all managed types, or maybe all embeddables. not just entities. // TODO it would be better to code-generate an Attribute.asJson() method than to do this at runtime if (eem.getMetamodel().getEntities().contains(attributeType)) { ErraiIdentifiableType<Y> attrEntityType = eem.getMetamodel().entity(attributeType); return attrEntityType.toJson(eem, attrValue); } return JsonUtil.basicValueToJson(attrValue); }
public JSONValue toJson(EntityManager em, X sourceEntity) { final ErraiEntityManager eem = (ErraiEntityManager) em; JSONObject jsonValue = new JSONObject(); for (Attribute<? super X, ?> a : getAttributes()) { ErraiAttribute<? super X, ?> attr = (ErraiAttribute<? super X, ?>) a; switch (attr.getPersistentAttributeType()) { case ELEMENT_COLLECTION: case EMBEDDED: case BASIC: jsonValue.put(attr.getName(), makeInlineJson(sourceEntity, attr, eem)); break; case MANY_TO_MANY: case MANY_TO_ONE: case ONE_TO_MANY: case ONE_TO_ONE: JSONValue attributeValue; if (attr instanceof ErraiSingularAttribute) { attributeValue = makeJsonReference(sourceEntity, (ErraiSingularAttribute<? super X, ?>) attr, eem); } else if (attr instanceof ErraiPluralAttribute) { attributeValue = makeJsonReference(sourceEntity, (ErraiPluralAttribute<? super X, ?, ?>) attr, eem); } else { throw new PersistenceException("Unknown attribute type " + attr); } jsonValue.put(attr.getName(), attributeValue); } } return jsonValue; }
@Override public <X, Y> void set(Attribute<X, Y> attribute, X entity, Y value) { ((ErraiAttribute<X, Y>) attribute).set(entity, value); }
@Override public <X, Y> Y get(Attribute<X, Y> attribute, X entity) { return ((ErraiAttribute<X, Y>) attribute).get(entity); }
for (Attribute<? super X, ?> a : getAttributes()) { ErraiAttribute<? super X, ?> attr = (ErraiAttribute<? super X, ?>) a; switch (attr.getPersistentAttributeType()) { case ELEMENT_COLLECTION: case EMBEDDED:
for (Attribute<? super X, ?> a : getAttributes()) { ErraiAttribute<? super X, ?> attr = (ErraiAttribute<? super X, ?>) a; JSONValue attrJsonValue = jsonValue.isObject().get(attr.getName()); switch (attr.getPersistentAttributeType()) { case ELEMENT_COLLECTION: case EMBEDDED: