- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我构建了一个使用 spring-data 的项目,并成功创建实体、添加与属性的关系。除了在持久化关系属性值后更新它们之外,一切似乎都运行良好。
为了尝试探索它,我合并了 SpringData 文档中的简单“世界”示例并对其进行了一些增强。在这个例子中,有一个与其他世界相关的世界实体。我向此关系添加了一个名为“yearsToReach”的属性。
在我的测试中,我创建了两个世界:火星和地球,并将yearsToReach 设置为10。然后,我调用 WorldRepositoryService.updateYearsToOtherWorld 并将yearsToReach 设置为 20。此方法以第一个 World 的“保存”结束,但当再次查询 World 时,它似乎并没有真正更改数据库中的值。
也许这真的很简单,但我只是花了几个小时试图弄清楚我做错了什么......
这是我的代码,我删除了一些不相关的部分。我真的很感谢任何回复/代码示例。
这是我的“World”实体(注意:“BaseWorld”类未标记为@NodeEntity,它仅包含“name”属性)。
@NodeEntity @Getter @Setter public class World extends BaseWorld { @GraphId private Long id; @RelatedToVia(type = OtherWorldRelationship.RELATIONSHIP_TYPE, direction = Direction.OUTGOING, elementClass = OtherWorldRelationship.class) private Set reachableByRocket = new HashSet(); public void addRocketRouteTo(World otherWorld, int yearsToReach) { OtherWorldRelationship otherWorldRelationship = new OtherWorldRelationship(); otherWorldRelationship.setWorld(this); otherWorldRelationship.setOtherWorld(otherWorld); otherWorldRelationship.setYearsToReach(yearsToReach); reachableByRocket.add(otherWorldRelationship); } public void updateYearsToOtherWorld(World otherWorld, int years) { Iterator otherWorlds = reachableByRocket.iterator(); while (otherWorlds.hasNext() ) { OtherWorldRelationship otherWorldRelationship = otherWorlds.next(); if (otherWorld.getName().equals(otherWorldRelationship.getOtherWorld().getName())){ otherWorldRelationship.setYearsToReach(years); break; } } }
这是关系类:
@RelationshipEntity @Getter @Setter public class OtherWorldRelationship { public static final String RELATIONSHIP_TYPE = "OtherWorld"; @GraphId Long id; @StartNode private World world; @EndNode private World otherWorld; private int yearsToReach; }
这是 WorldRepository 接口(interface) - 它仅继承自 GraphRepository
public interface WorldRepository extends GraphRepository, NamedIndexRepository { }
这是WorldRepositoryService:
@Repository public class WorldRepositoryService implements IWorldRepositoryService { @Getter @Setter @Autowired private WorldRepository worldRepository; @Override @Transactional public Collection createSomeWorlds() { ArrayList newWorlds = new ArrayList(); World earth = createWorld("Earth"); newWorlds.add(earth); World mars = createWorld("Mars"); mars.addRocketRouteTo(earth, 10); newWorlds.add(mars); return newWorlds; } @Override @Transactional public World createWorld(String name) { return worldRepository.save(new World(name)); } @Override public World findWorldNamed(String name) { return worldRepository.findByPropertyValue("name", name); } @Override @Transactional public World updateYearsToOtherWorld(World mars, World earth, int years) { mars.updateYearsToOtherWorld(earth, years); return worldRepository.save(mars); } }
最后,这些是我的测试中的行:
@Test public void testNeo4JRelationshipUpdateData() { Iterable worlds = worldRepositoryService.createSomeWorlds();//Mars and Earth World earth = worldRepositoryService.findWorldNamed("Earth"); World mars = worldRepositoryService.findWorldNamed("Mars"); Integer distanceInYears = mars.getYearsToOtherWorld(earth); System.out.println("Distance beteween Mars and " + distanceInYears); Assert.assertEquals(10, distanceInYears.intValue()); mars = worldRepositoryService.updateYearsToOtherWorld(mars, earth, 20); System.out.println("Distance beteween Mars and Earth: " + distanceInYears); Assert.assertEquals(20, distanceInYears.intValue()); mars = worldRepositoryService.findWorldNamed("Mars"); distanceInYears = mars.getYearsToOtherWorld(earth); System.out.println("Distance beteween Mars and Earth after update: " + distanceInYears); // !!! This line fails - it gets 10 instead of 20 !!! // Assert.assertEquals(20, distanceInYears.intValue()); }
最佳答案
感谢 jjaderberg 的帮助 - 我为了其他人而粘贴解决方案:
解决方案是保存关系本身,而不是保存关系的实体。这是固定代码:
我为关系定义了新的存储库接口(interface):
public interface OtherWorldRelationshipRepository extends GraphRepository, NamedIndexRepository { }
在 WorldRepositoryService.clas 中,我保存了关系而不是实体:
@Override @Transactional public OtherWorldRelationship updateYearsToOtherWorld(World mars, World earth, int years) { OtherWorldRelationship yearsToOtherWorldRelation = mars.updateYearsToOtherWorld(earth, years); return otherWorldRelationshipRepository.save(yearsToOtherWorldRelation); }
就是这样!!希望它能帮助别人就像帮助我自己一样。
卡梅尔
关于java - 如何更新 spring-data-neo4j 中的关系属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19812010/
我是一名优秀的程序员,十分优秀!