Spring in Practice

Willie Wheeler's Spring blog

Initializing Lazy-loaded Collections With Spring Data Neo4j

| Comments

Spring Data Neo4j has been around for about a year now, but the so-called “simple mapping” strategy (the one that doesn’t use AspectJ) is new. So it doesn’t have some of the bells and whistles that you might expect to see if you’re coming from, say, Hibernate. In particular, it doesn’t (as of Spring Data Neo4j 2.0.0.RELEASE) use proxies for collections.

The question of how to initialize lazily loaded collections came up on StackOverflow. Michael Hunger (Spring Data Neo4j lead) explained that you can use the Neo4jTemplate’s fetch() method to perform the initialization. Here’s some code showing how to do it:

package org.skydingo.skybase.service.impl;

import javax.inject.Inject;
import org.skydingo.skybase.model.Person;
import org.skydingo.skybase.repository.PersonRepository;
import org.skydingo.skybase.service.PersonService;
import org.springframework.data.neo4j.support.Neo4jTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

public class PersonServiceImpl implements PersonService {
    @Inject private Neo4jTemplate template;
    @Inject private PersonRepository personRepo;

    public Person findPersonDetails(Long id) {
        Person person = personRepo.findOne(id);
        return person;

    ... other methods ...

Have fun.