Database Reference
In-Depth Information
•
public <U extends T> Iterable<U> save(Iterable<U> en-
tities);
•
public User findOne(Long id);
•
public boolean exists(Long id);
•
public Result<User> findAll();
•
public void delete(User entity);
SDN builds on the abstraction concept defined in the Spring Data Commons module that
provides shared infrastructure across Spring Data projects. SDN takes on the responsib-
ility of implementing the base methods in the interface that your repository extends (for
example, the
GraphRepository
interface in the
UserRepository
case), providing
a uniform set of functionality to support common loading, saving, querying, indexing, and
traversing operations specific to an entity.
The following listing depicts how the
UserRepository
can be used to save and load
users.
Listing 9.9. Loading and saving data via the
UserRepository
@Autowired
UserRepository userRepository;
@Transactional
public void saveAndLoad() {
User user = new User("john001","John");
User savedUser = userRepository.save(user);
User loadedUser = userRepository.findOne(savedUser.getNodeId());
User loadedUserViaIndex =
userRepository.findBySchemaPropertyValue ("userId","john001");
}
You'll notice that this code is very similar to that used by the
Neo4jTemplate
approach
in
listing 9.7
(with the exception that you don't have to provide a target type). The
Re-
pository.save
method persists the entity into the graph, with the loading being ac-
complished with
findOne
(based on a node ID) or
findBySchemaPropertyValue
(based on an index lookup, against the
userId
property in this case). For Spring to know
about any repository interfaces you define, it needs to be told. Adding the following code
into your Spring XML configuration file will do the trick:
<neo4j:repositories base-
package="com.manning.neo4jia.chapter09simple.repository"/>