forked from spring-projects/spring-data-neo4j
-
Notifications
You must be signed in to change notification settings - Fork 0
/
developer_notes.html
229 lines (207 loc) · 7.97 KB
/
developer_notes.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<html>
<head>
<title>Spring Data Neo4j - Developer Notes</title>
<style type="text/css">
body {
background-color: #add8e6;
}
.note {
background-color: #fafad2;
border: 3px ridge black;
/*position: absolute;*/
padding: 1em;
max-width: 30em;
display: inline-block;
}
.note h3 {
font-style: italic;
font-family: serif;
}
</style>
<style type="text/css" media="print">
.page {
page-break-after:always;
}
body {
margin-left: 3em;
}
</style>
</head>
<body>
<h1 style="text-align: center">Developer Notes</h1>
<div style="padding:1em;">
<img src="http://neo4j.org/wp-content/uploads/2011/09/springdataNeo4j-logo-cmyk-11.png" alt="Spring Data Neo4j Logo"/>
</div>
<div class="note">
<h3>Neo4j - [IS_A] -> Property Graph</h3>
<p><strong>Nodes</strong> with key-value properties are connected by <strong>typed</strong>, directed <strong>relationships</strong> with properties.</p>
<em>TODO put in a pretty picture</em>
<h4>Cypher is a query language:</h4>
<pre>
start movie=(id) match movie<-[:ACTS_IN]-actor
where actor.age > 20 return movie, actor.name
order by movie.title </pre>
<h4>Traversals walk the graph in the prescribed way</h4>
<pre>
TraversalDescription actors = Traversal.description()
.relationships("ACTS_IN").filter(returnAllButStartNode());
for (Node actor : actors.traverse(movie))
...</pre>
<h4>Transactional: TX needed for write operations</h4>
<pre>
try (Transaction tx=graphDatabaseService.beginTx()) {
... graph operations ...
tx.success();
} catch(Exception e) {
tx.failure();
}
</pre>
</div>
<div class="note">
<h3> Maven Dependency</h3>
<pre>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j[-aspects]</artifactId>
<version>2.0.0.RC1</version>
</dependency>
</pre>
</div>
<div class="note">
<h3>Spring-Config</h3>
<pre><beans ...
xmlns:neo4j="http://www.swf.org/schema/data/neo4j"
xsi:schemaLocation=
"http://www.sfw.org/schema/data/neo4j" ...>
...
<neo4j:config store-dir="graph.db"/>
<neo4j:repositories base-package="com.example.repositories"/>
...
</beans></pre>
<h4>Testing</h4>
<pre><neo4j:config graphDatabaseService="gds"/>
<bean id="gds" class="...ImpermanentGraphDatabase"/></pre>
<h4>REST</h4>
<pre><neo4j:config graphDatabaseService="rds"/>
<bean id="rds" class="...SpringRestGraphDatabase"/></pre>
</div>
<div class="note">
<h3>Entity - Annotations</h3>
<ul>
<li><code>@NodeEntity</code> marks a domain class as backed by a node in the graph</li>
<li><code>@RelationshipEntity</code> marks a domain class as backed by a relationship in the graph</li>
<li><code>@GraphId</code> Field for id</li>
<li><code>@Indexed([fulltext=true],[indexName=name])</code> Auto-Indexing</li>
<li><code>@RelatedTo([direction=Direction], [type=TYPE])</code> define type and direction of relationship on <code>Entity, Iterable<E>, Set<E></code> fields</li>
<li><code>@RelatedToVia([direction=Direction], [type=TYPE])</code> like <code>@RelatedTo,</code> but targets Relationship-Entities</li>
<li><code>@Query(query, [type=QueryType])</code> computed field, executes graph query in Cypher</li>
<li><code>@GraphTraversal(traversal=~TraversalBuilder.class, [elementClass=Person.class])</code> computed field, executes traversal starting from entity-node</li>
</ul>
</div>
<div class="note">
<h3>Example</h3>
<pre>
@NodeEntity
public class Movie {
@GraphId Long id;
@Indexed(fulltext = true, indexName = "search")
String title;
Person director;
@RelatedTo(type="ACTS_IN", direction = INCOMING)
Set<Person> actors;
@RelatedToVia(type = "RATED")
Iterable<Rating> ratings;
@Query("start movie=node({self}) match
movie-->genre<--similar return similar")
Iterable<Movie> similarMovies;
}</pre>
</div>
<div class="note">
<h3>Repositories</h3>
<pre>
interface MovieRepository extends GraphRepository<Movie> {
@Query("start movie={0} match m<-[rating:RATED]-user
return rating")
Iterable<Rating> getRatings(Movie movie);
// Co-Actors
Iterable<Person> findByActorsMoviesActorName(name)
}
<neo4j:repositories base-package="com.example.dao"/></pre>
<ul>
<li>Provide CRUD, Finder, Query and Traversal abilities by default.</li>
<li>Automatic Finders can be derived from method names (like (G)Rails) or @Query annotations.</li>
<li>custom method implementations possible</li>
</ul>
</div>
<div class="note">
<h3>Neo4jTemplate</h3>
<ul><li>convenience methods for graph operations, transaction and exception handling</li>
<li>index lookups, cypher queries, traversal execution with fluent result converter API</li>
<li>preconfigured and customizable conversion of results to a variety of types</li></ul>
</div>
<div class="note">
<h3>AspectJ - ActiveRecordMixin</h3>
adds the following methods, when implementing the NodeBacked interface
<pre>
getPersistentState()
relateTo(target, ...),
getRelationshipTo(...)
persist(),
remove(),
detach()
...</pre>
</div>
<div class="note">
<h3>Roo Add-on</h3>
<pre>
roo> addon search graph
roo> addon install id --searchResultId 01
roo> project --topLevelPackage net.cineasts
roo> graph setup --provider NEO4J --databaseLocation cineasts
roo> graph entity --class ~.model.Movie
roo> field string title
roo> graph entity --class ~.model.Actor
roo> field string name
roo> graph relationship --to Movie --from Actor
--fieldName movies --type ACTS_IN --cardinality ONE_TO_MANY
roo> controller all --package ~.web</pre>
</div>
<div class="note">
<h3>Libraries</h3>
<h4>spring-data-neo4j</h4>
Provides Repository based Object <-> Graph Mapping that copies data from and to the graph database.
<h4>spring-data-neo4j-aspects</h4>
Provides transparent property and relationship mapping inside of transactions with live read- and write-through to the graph database.
<h4>spring-data-neo4j-cross-store</h4>
Allows to partially store properties and relationships of JPA entities in the graph database.
<h4>spring-data-neo4j-rest</h4>
Provides transparent remote access to Neo4j-Server via its REST Api (uses <code>org.neo4j:neo4j-rest-graphdb</code>)
</div>
<div class="note">
<h3>AspectJ - STS / eclipse setup</h3>
<ul>
<li>STS 2.7.1</li>
<li>import existing maven project or set aspectj nature</li>
<li>Project Properties -> AspectJ -> Aspect Paths</li>
<li>AspectJ Configurator installed</li>
<li>sometimes build after restart required</li></ul>
</div>
<div class="note">
<h3>Links</h3>
<ul style="list-style: none">
<li><a href="http://neo4j.org">http://neo4j.org</a></li>
<li><a href="http://spring-neo4j.org/">http://spring-neo4j.org/</a></li>
<li><a href="http://spring-neo4j.org/guide">http://spring-neo4j.org/guide</a></li>
<li><a href="http://spring-neo4j.org/discussions">http://spring-neo4j.org/discussions</a></li>
<li><a href="http://spring-neo4j.org/docs">http://spring-neo4j.org/docs</a></li>
<li><a href="http://spring-neo4j.org/download">http://spring-neo4j.org/download</a></li>
<li><a href="http://spring-neo4j.org/issues">http://spring-neo4j.org/issues</a></li>
<li><a href="http://spring-neo4j.org/source">http://spring-neo4j.org/source</a></li>
<li><a href="http://cineats.net">http://cineasts.net</a></li>
</ul>
</div>
<div style="background-color: white;display: inline-block;">
<a href="http://spring.neo4j.org" target="_blank"><img width="200" height="200" src="http://spring.neo4j.org/qr"/></a>
</div>
</body>
</html>