编辑: 在github上有示例项目可供参考。
我们的后端项目中使用了Neo4J(Rest图形数据库,托管在grapheneDb中)和Spring Data。
<bean id="graphDatabaseService" class="org.springframework.data.neo4j.rest.SpringCypherRestGraphDatabase">
我有两个实体之间的简单一对多关系:
User
和Stay
。
编辑:我认为这与问题无关,但在SDN4中看到了一个类似的问题后,我认为需要更新问题(有一个基本的@NodeEntity
类,两个实体都扩展了这个基类)。@NodeEntity
public abstract class BasicNodeEntity implements Serializable {
@GraphId
private Long nodeId;
}
public class User extends BasicNodeEntity {
@RelatedTo(type = "HAS_STAY", direction = Direction.OUTGOING)
Set<Stay> stays;
public void addStay(Stay stay) {
stays.add(stay);
}
}
public class Stay extends BasicNodeEntity {
@RelatedTo(type = "HAS_STAY", direction = Direction.INCOMING)
User user;
}
我无法持续超过一个逗留时间。我添加到用户的第一个逗留时间被正确地持久化,但只有第一个逗留时间。添加的下一个逗留时间从不持久化,我总是检索到第一个逗留时间。
我用来创建新逗留时间的方法是:
@Autowired
Neo4jOperations template;
@Transactional
private void createStay(Stay stay, User user) throws Exception {
stay = template.save(stay);
user.addStay(stay);
template.save(user);
// If i evaluate user at this point, it contains both stays
// But if I retrieve the user from the repository, it just contains
// the first stay, the second one has not persisted.
}
编辑:通过UserRepository
正确检索到修改后的用户。public interface UserRepositoryCustom {}
public interface UserRepository extends GraphRepository<User>, UserRepositoryCustom {
User findById(String id);
}
User user = userRepository.findById(userId);
注意:我也尝试通过存储库接口而不是Neo4jTemplate来保存,但是出现了相同的问题。
这两个实体都正确保存在neo4j数据库中,只是一个持久性问题。
我认为这应该很容易,所以我可能错过了什么...
任何帮助将不胜感激。
相关版本:
<spring.version>4.0.5.RELEASE</spring.version>
<spring-data-neo4j.version>3.3.2.RELEASE</spring-data-neo4j.version>
有另一个类似问题的SO问题,但迄今没有回应。
SpringCypherRestGraphDatabase
(托管在grapheneDB中)。 - troig