/** * 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); }