0

I have a simple relationship (Account)-[IdentifiedBy]->(Identity), defined like this

@RelatedTo(type = "IDENTIFIED_BY", direction = Direction.OUTGOING)
private Set<Identity> identities = new HashSet<Identity>();

When I load the Account and access its identities, all the identities are loaded, but all their properties except for ID are null. However, if I annotate the property with @Fetch, then the identities are loaded correctly, with all properties. Is this by design or am I missing something?

@NodeEntity
public class Account {
    @GraphId Long nodeId;

    @RelatedTo(type = "IDENTIFIED_BY", direction = Direction.OUTGOING)
    //@Fetch
    private Set<Identity> identities = new HashSet<Identity>();

    public Set<Identity> getIdentities() {
        return identities;
    }

    public void setIdentities(Set<Identity> identities) {
        this.identities = identities;
    }
}

@NodeEntity
public class Identity {
    @GraphId Long nodeId;

    private String identifier;

    public String getIdentifier() {
        return identifier;
    }

    public void setIdentifier(String identifier) {
        this.identifier = identifier;
    }
}

public interface AccountRepository extends GraphRepository<Account> { }

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/test-context.xml"})
@Transactional
public class AccountTests {

    @Autowired
    protected AccountRepository accountRepository;

    @Test
    public void accountMustLoadItsIdentities() {
        Account acct = accountRepository.save(new Account());

        Identity id = new Identity();
        id.setIdentifier("merlin");
        acct.getIdentities().add(id);
        accountRepository.save(acct);

        acct = accountRepository.findOne(acct.nodeId);
        id = acct.getIdentities().iterator().next();
        assertEquals("merlin", id.getIdentifier());
    }    
}

The unit test fails on the last assertion, but succeeds if @Fetch on Account is uncommented.

4

2 回答 2

2

而不是使用

account.getIdentities()

您应该执行以下操作:

this.neo4jTemplate.fetch(account.getIdentities())

不使用 @Fetch 关键字不会自动启用延迟加载。要延迟加载您的属性,请使用如上图所示的 Neo4jTemplate。

于 2013-08-25T10:12:30.083 回答
0

这是设计使然

我们试图通过不急切地遵循关系来避免将整个图加载到内存中。一个专用的 @Fetch 注释控制是否加载相关实体。每当一个实体没有完全加载时,就只存储它的 id。然后可以使用 template.fetch() 操作显式加载这些实体或实体集合。

http://static.springsource.org/spring-data/data-graph/snapshot-site/reference/html/#reference:simple-mapping

于 2013-08-29T10:39:41.353 回答