1

What is the proper way to convert StatementResult of Neo4j session execution to a proper object or at least a Json assuming I don't have access to SessionFactory ?

I am doing it like this and it looks clumsy and verbose:

var session = require(Driver.class).session(); // v1 Driver, v1 Session
try (session) {
    Iterable<Record> iterable =
            () -> session.run(FIND_BY_TITLE_LIKE, Map.of("titleLike", ".*" + titleLike + ".*"));
    var list = StreamSupport
            .stream(iterable.spliterator(), false)
            .map(Record::asMap)
            .map(Map::values)
            .flatMap(Collection::stream) // nodes
            .map(node -> ((InternalNode) node).asMap())
            .collect(Collectors.toList());
    LOGGER.info("Processed list: {}", list);
    return list;
}

Maybe I should some other Driver or Session for that?

lapots
  • 12,553
  • 32
  • 121
  • 242
  • And why not using Neo4j-OGM ? It's the goal this project ... Otherwise you can write a function that convert a `Record` to `JSON` (or whatever) and put it in the first `Map` function (remove all the thing after) – logisima Oct 12 '18 at 12:28
  • @logisima ok, I'll try – lapots Oct 12 '18 at 12:31

1 Answers1

0

You can use Spring-Data-Neo4j (SDN) to map query results to domain entities. You just need the following three code snippets.

Repository

@Repository
public interface NodeEntityNameDAO extends Neo4jRepository<NodeEntityName, Long> {
  @Query("MATCH (nodeA:LabelA)<-[:CONTAINS]-(nodeB:LabelB) RETURN nodeA.name AS nodeAName, nodeB.name AS nodeBName;")
  NodeAAndBResult[] getNodeAAndB();
}

Result object

@QueryResult
public class NodeAAndBResult {
  private String nodeAName;
  private String nodeBName;

  // omitted default constructor as well getter and setter for clarity
}

Business logic

NodeAAndBResult[] nodeAAndBResults = nodeEntityNameDAO.getNodeAAndB();

Addendum

Just found a wonderful article by @Luanne from GraphAware for the same topic.

ThirstForKnowledge
  • 1,245
  • 1
  • 10
  • 27