gjrwebber / spring-data-gremlin

Spring data gremlin makes it easier to implement Graph based repositories. This module extends Spring Data to allow support for potentially any Graph database that implements the Tinkerpop Blueprints 2.x API.
69 stars 54 forks source link

EdgeRepository - update Edge #42

Closed clepelli closed 8 years ago

clepelli commented 8 years ago

Hello again,

Updating an edge via the SimpleGremlinRepository always fails, throwing "Schema is neither EDGE nor VERTEX!".

Actually there is a typo there: https://github.com/gjrwebber/spring-data-gremlin/blob/126efb2529e5f362222b2f15fe246de764c90e97/spring-data-gremlin-core/src/main/java/org/springframework/data/gremlin/repository/SimpleGremlinRepository.java#L120

Best regards, Clément.

clepelli commented 8 years ago

Then, we should remove the cascade when the Edge is updated as the vertices may not be loaded. Below is my quick local rewriting of the method discussed above.

@Transactional(readOnly = false)
public T save(Graph graph, T object) {

    String id = schema.getObjectId(object);
    if (StringUtils.isEmpty(id)) {
        create(graph, object);
    } else {
        Element element;
        if (schema.isVertexSchema()) {
            element = graph.getVertex(schema.decodeId(id));
            schema.copyToGraph(graphAdapter, element, object);
        } else if (schema.isEdgeSchema()) {
            element = graph.getEdge(schema.decodeId(id));
            Object outObject = ((GremlinEdgeSchema) schema).getOutProperty().getAccessor().get(object);
            Object inObject = ((GremlinEdgeSchema) schema).getInProperty().getAccessor().get(object);
            schema.copyToGraph(graphAdapter, element, object, outObject, inObject);
        } else {
            throw new IllegalStateException("Schema is neither EDGE nor VERTEX!");
        }
        if (element == null) {
            throw new IllegalStateException(String.format("Could not save %s with id %s, as it does not exist.", object, id));
        }
    }
    return object;
}
gjrwebber commented 8 years ago

Fixed in 11ee40b. Thanks clepelli!