updated tutorial documentation to new repository approach

This commit is contained in:
Michael Hunger
2011-04-04 05:12:35 +02:00
parent 07c57e9fe7
commit d8a732d92b
6 changed files with 25 additions and 28 deletions

View File

@@ -83,7 +83,7 @@
<programlisting language="java" ><![CDATA[
@Transactional
public Movie importMovie(String movieId) {
Movie movie = moviesRepository.getMovie(movieId);
Movie movie = repository.getMovie(movieId);
if (movie == null) { // Not found: Create fresh
movie = new Movie(movieId,null);
}

View File

@@ -5,7 +5,7 @@
<para>
There an @Indexed annotation for fields. We wanted to try this out, and use it to guide the next test. We added an @Indexed to the id field of the movie.
This field is intended to represent the external id that will be used in URIs and will stable over database imports and updates. This time we went with the
Finder to retrieve the indexed movie.
default NodeGraphRepository (previously Finder) to retrieve the indexed movie.
</para><para>
@@ -18,12 +18,12 @@ class Movie {
int year;
}
@Autowired FinderFactory graphRepositoryFactory;
@Autowired DirectGraphRepositoryFactory graphRepositoryFactory;
@Test [@Transactional] public void persistedMovieShouldBeRetrievableFromGraphDb() {
int id=1;
Movie forrestGump = new Movie(id, "Forrest Gump", 1994).persist();
NodeFinder<Movie> movieFinder = graphRepositoryFactory.createNodeEntityFinder(Movie.class);
NodeGraphRepository<Movie> movieFinder = graphRepositoryFactory.createNodeEntityRepository(Movie.class);
// REMINDER, the "null" stands for an optional index name
Movie retrievedMovie = movieFinder.findByPropertyValue(null, "id",id);
assertEqual("retrieved movie matches persisted one",forrestGump,retrievedMovie);

View File

@@ -3,22 +3,26 @@
<chapter id="tutorial_repository">
<title>Serving a good cause - Repository</title>
<para>
That was the first method to add to the brand new repository. So we created a repository for the application, annotated
it with @Repository and @Transactional. We did the same for the Actor.
That was the first method to add to the brand new cineasts repository.
First step was to create an (still empty) repository interface for Movie (and Actor). We added the repository configuration
to our application context.
Then we created a repository for the application, annotated it with @Repository and @Transactional and injected the movie repository.
We did the same for the Actor.
</para>
<para>
<programlisting language="java"><![CDATA[
public interface MovieRepository extends NodeGraphRepository<Movie> {
// findById(String id) - automatic derived finder for a future SDG release
}
<datagraph:repositories base-package="org.neo4j.cineasts.repository"/>
@Repository @Transactional
public class CineastsRepostory {
FinderFactory graphRepositoryFactory;
Finder<Movie> movieFinder;
@Autowired
public CineastsRepostory(FinderFactory graphRepositoryFactory) {
this.graphRepositoryFactory = graphRepositoryFactory;
this.movieFinder = graphRepositoryFactory.createNodeEntityFinder(Movie.class);
}
@Autowired MovieRepository movieRepository;
public Movie getMovie(int id) {
return movieFinder.findByPropertyValue(null,"id", id);
return movieRepository.findByPropertyValue(null,"id", id);
}
}
]]></programlisting>

View File

@@ -15,13 +15,12 @@
public class DatabasePopulator {
@Autowired GraphDatabaseContext ctx;
@Autowired MoviesRepository repository;
@Autowired CineastsRepository repository;
@Transactional
public List<Movie> populateDatabase() {
Actor tomHanks = new Actor("1", "Tom Hanks").persist();
Movie forestGump = new Movie("1", "Forrest Gump").persist();
forestGump.setYear(1994);
tomHanks.playedIn(forestGump,"Forrest");
return asList(forestGump);
}}

View File

@@ -90,13 +90,7 @@ in a separate applicationContext-security.xml. But first, as always, Maven and w
@Service
public class CineastsUserDetailsService implements UserDetailsService, InitializingBean {
@Autowired private FinderFactory graphRepositoryFactory;
private NodeFinder<User> userFinder;
@Override
public void afterPropertiesSet() throws Exception {
userFinder = graphRepositoryFactory.createNodeEntityFinder(User.class);
}
@Autowired private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String login) throws UsernameNotFoundException, DataAccessException {
@@ -106,7 +100,7 @@ public class CineastsUserDetailsService implements UserDetailsService, Initializ
}
public User findUser(String login) {
return userFinder.findByPropertyValue("users","login",login);
return userRepository.findByPropertyValue(null,"login",login);
}
public User getUserFromSession() {
SecurityContext context = SecurityContextHolder.getContext();

View File

@@ -9,7 +9,7 @@ and cast in a jsp was straightforward. Actually just using the repository to loo
<programlisting language="java"><![CDATA[
@RequestMapping(value = "/movies/{movieId}", method = RequestMethod.GET, headers = "Accept=text/html")
public String singleMovieView(final Model model, @PathVariable String movieId) {
Movie movie = moviesRepository.getMovie(movieId);
Movie movie = repository.getMovie(movieId);
model.addAttribute("id", movieId);
if (movie != null) {
model.addAttribute("movie", movie);
@@ -40,9 +40,9 @@ searching movies. To restrict the size of the returned set we just added a limit
</para>
<para>
<programlisting language="java"><![CDATA[
public void List<Movie> searchForMovie(String query, int count) {
public void List<Movie> findMovies(String query, int count) {
List<Movie> movies=new ArrayList<Movie>(count);
for (Movie movie : movieFinder.findAllByQuery("title", query)) {
for (Movie movie : movieRepository.findAllByQuery("title", query)) {
movies.add(movie);
if (count-- == 0) break;
}
@@ -62,7 +62,7 @@ domain classes.
<programlisting language="java"><![CDATA[
@RequestMapping(value = "/movies", method = RequestMethod.GET, headers = "Accept=text/html")
public String findMovies(Model model, @RequestParam("q") String query) {
List<Movie> movies = moviesRepository.findMovies(query, 20);
List<Movie> movies = repository.findMovies(query, 20);
model.addAttribute("movies", movies);
model.addAttribute("query", query);
return "/movies/list";