@@ -48,27 +48,29 @@ public void close() throws Exception {
48
48
driver .close ();
49
49
}
50
50
51
- public void createFriendship (final String person1Name , final String person2Name ) {
51
+ public void createFriendship (final String person1Name , final String person2Name , final String knowsFrom ) {
52
52
// To learn more about the Cypher syntax, see https://neo4j.com/docs/cypher-manual/current/
53
53
// The Reference Card is also a good resource for keywords https://neo4j.com/docs/cypher-refcard/current/
54
54
String createFriendshipQuery = "CREATE (p1:Person { name: $person1_name })\n " +
55
55
"CREATE (p2:Person { name: $person2_name })\n " +
56
- "CREATE (p1)-[:KNOWS]->(p2)\n " +
57
- "RETURN p1, p2" ;
56
+ "CREATE (p1)-[k :KNOWS { from: $knows_from } ]->(p2)\n " +
57
+ "RETURN p1, p2, k " ;
58
58
59
59
Map <String , Object > params = new HashMap <>();
60
60
params .put ("person1_name" , person1Name );
61
61
params .put ("person2_name" , person2Name );
62
+ params .put ("knows_from" , knowsFrom );
62
63
63
64
try (Session session = driver .session ()) {
64
65
// Write transactions allow the driver to handle retries and transient errors
65
66
Record record = session .writeTransaction (tx -> {
66
67
Result result = tx .run (createFriendshipQuery , params );
67
68
return result .single ();
68
69
});
69
- System .out .println (String .format ("Created friendship between: %s, %s" ,
70
+ System .out .println (String .format ("Created friendship between: %s, %s from %s " ,
70
71
record .get ("p1" ).get ("name" ).asString (),
71
- record .get ("p2" ).get ("name" ).asString ()));
72
+ record .get ("p2" ).get ("name" ).asString (),
73
+ record .get ("k" ).get ("from" ).asString ()));
72
74
// You should capture any errors along with the query and data for traceability
73
75
} catch (Neo4jException ex ) {
74
76
LOGGER .log (Level .SEVERE , createFriendshipQuery + " raised an exception" , ex );
@@ -102,9 +104,12 @@ public static void main(String... args) throws Exception {
102
104
103
105
String user = "<Username for Neo4j Aura database>" ;
104
106
String password = "<Password for Neo4j Aura database>" ;
105
-
106
- try (DriverIntroductionExample app = new DriverIntroductionExample (boltUrl , user , password , Config .defaultConfig ())) {
107
- app .createFriendship ("Alice" , "David" );
107
+ Config config = Config .builder ()
108
+ // Configuring slf4j logging
109
+ .withLogging ( Logging .slf4j () )
110
+ .build ();
111
+ try (DriverIntroductionExample app = new DriverIntroductionExample (boltUrl , user , password , config )) {
112
+ app .createFriendship ("Alice" , "David" , "School" );
108
113
app .findPerson ("Alice" );
109
114
}
110
115
}
0 commit comments