Skip to content

Commit

Permalink
docs: Add example how to turn Node into Map.
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-simons committed Nov 14, 2024
1 parent 75d41a8 commit 52133da
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
package org.neo4j.jdbc.it.sb;

import java.util.List;
import java.util.Map;

import org.neo4j.jdbc.values.Node;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -43,4 +45,13 @@ public int createOrUpdate(Movie movie) {
return this.jdbcTemplate.update("INSERT INTO Movie(title) VALUES(?) ON DUPLICATE KEY IGNORE", movie.title());
}

public Map<String, Object> findAsMap(String title) {
// Magic comment just added because I configured the driver here to always assume SQL by default
// SQL parameters start at 1, though
return this.jdbcTemplate.queryForObject(
"/*+ NEO4J FORCE_CYPHER */ MATCH (p:Movie) WHERE p.title = $1 RETURN p",
Node.class, title
).asMap();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ void shouldNotFailOnMerge(@Autowired MovieRepository movieRepository) {
assertThat(movieRepository.createOrUpdate(new Movie("00 Schneider – Jagd auf Nihil Baxter"))).isEqualTo(0);
}

@Test
void shouldReturnAMap(@Autowired MovieRepository movieRepository) {
assertThat(movieRepository.findAsMap("Barbieheimer")).containsExactlyEntriesOf(Map.of("title", "Barbieheimer"));
}

@SuppressWarnings("unchecked")
@AfterAll
static void makeSureEverythingHasBeenRolledBack(@Autowired Neo4jHttpClient httpClient) {
Expand Down

0 comments on commit 52133da

Please sign in to comment.