我有一個簡單的關係(帳戶) - [IdentifiedBy] - >(身份),像這樣SpringData Neo4j的節點屬性
@RelatedTo(type = "IDENTIFIED_BY", direction = Direction.OUTGOING)
private Set<Identity> identities = new HashSet<Identity>();
定義。當我加載賬戶並訪問其身份,所有身份被加載,但除ID之外的所有屬性均爲空。但是,如果我使用@Fetch註解屬性,那麼身份將正確加載,並具有所有屬性。這是由設計還是我錯過了什麼?
@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());
}
}
單元測試失敗的最後一個斷言,但成功,如果帳戶@Fetch未註釋。
這是由設計。如果你想要熱切地提取關係,你需要註解。如果您不想使用註釋,請查看文檔中的高級映射模式。 – tstorms