Chapter 7. Do I know you? - Indexing

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.

@NodeEntity
class Movie {
    @Indexed
    int id;
    String title;
    int year;
}

@Autowired FinderFactory finderFactory;

@Test [@Transactional] public void persistedMovieShouldBeRetrievableFromGraphDb() {
    int id=1;
    Movie forrestGump = new Movie(id, "Forrest Gump", 1994).persist();
    NodeFinder<Movie> movieFinder = finderFactory.createNodeEntityFinder(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);
    assertEqual("retrieved movie title matches","Forrest Gump",retrievedMovie.getTitle());
}

Surprisingly, this failed with an exception about not being in a transaction, which means we forgot to add the @Transactional annotation. That's easy enough to add to the test, and resume the test/code cycle.