Database Reference
In-Depth Information
Listing 9.8. A Neo4jTemplate example with full Spring integration
ThisnewcodecomesalotclosertoachievingyourSpringnirvanaofonlyneedingtowrite
the core business logic, deferring all low-level plumbing to Spring. The boilerplate trans-
action code has now been replaced with the @Transactional annotation that instructs
Spring to ensure that all code executed within the method is wrapped in a transaction for
you.
Focusing on the core persistence logic, you can see that creating and saving a new entity
is as simple as calling the save method on the Neo4jTemplate class, passing in
the newly created POJO-based entity as the argument. Internally, the Neo4jTemplate
creates a new node in the database for you, assigns a new (or reused) graph node ID, stores
all associated properties ( userId and name ), and indexes the userId property.
Listing 9.7 illustrates two ways to load an entity. The first requires knowledge of and
access to the graph node ID, which you have as a result of a previous call to save the en-
tity . Recall that the @GraphId- annotated nodeId property on the User entity was
requiredbySDNtostoretheunderlyingnodeID.You'resimplymakinguseofthisinform-
ation now for your own lookup purposes.
The second approach, for the cases where the node ID isn't available or known, is to look
up the entity via the indexed userId property
.
Search WWH ::




Custom Search