java – 如何在Spring数据neo4j中更新节点或节点实体?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 如何在Spring数据neo4j中更新节点或节点实体?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1951字,纯文字阅读大概需要3分钟。
内容图文
![java – 如何在Spring数据neo4j中更新节点或节点实体?](/upload/InfoBanner/zyjiaocheng/800/3f9f1b5bc83c4f22b751088d320cfa0d.jpg)
在Spring数据neo44中我们只有repository.save(实体),但是例如当我的UserEntity的属性(email)发生变化时,我不知道如何更新它.
我也尝试使用neo4j模板,但保存具有现有节点ID的实体导致下面的回滚.
org.springframework.dao.InvalidDataAccessApiUsageException: New value must be a Set, was: class java.util.ArrayList; nested exception is java.lang.IllegalArgumentException: New value must be a Set, was: class java.util.ArrayList
at org.springframework.data.neo4j.support.Neo4jExceptionTranslator.translateExceptionIfPossible(Neo4jExceptionTranslator.java:43)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
我们如何更新节点或节点实体?
public void updateUserNode(UserEntity user) {
try{
UserEntity updatedUser = this.getUserByUserId(user.getUserId());//finding node with user id///
updatedUser.setEmail(user.getEmail());
updatedUser.setImageId(user.getImageId());
updatedUser.setFirstname(user.getFirstname());
updatedUser.setLastname(user.getLastname());
//System.out.println("Deleting ");
//userRepository.delete(del);
System.out.println("UPDATING ");
// with existing Id, you can not save it again/, or update
updatedUser = userRepository.save(updatedUser);
}catch(Exception e){
e.printStackTrace();
}
//return
}
解决方法:
您必须在事务中嵌入.save().
举个例子:
final org.neo4j.graphdb.Transaction tx = this.neoTemplate.getGraphDatabaseService().beginTx();
try {
updatedUser = userRepository.save(updatedUser);
tx.success();
} finally {
tx.finish();
}
内容总结
以上是互联网集市为您收集整理的java – 如何在Spring数据neo4j中更新节点或节点实体?全部内容,希望文章能够帮你解决java – 如何在Spring数据neo4j中更新节点或节点实体?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。