Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何更新Spring数据neo4j中的节点或节点实体?_Java_Neo4j_Spring Data Neo4j - Fatal编程技术网

Java 如何更新Spring数据neo4j中的节点或节点实体?

Java 如何更新Spring数据neo4j中的节点或节点实体?,java,neo4j,spring-data-neo4j,Java,Neo4j,Spring Data Neo4j,在SpringDataNeo44中,我们只有repository.save(entity),但例如,当我的UserEntity的属性(email)发生更改时,我不知道如何更新它 我也尝试使用neo4j模板,但使用现有节点id保存实体会导致下面的回滚 org.springframework.dao.InvalidDataAccessApiUsageException: New value must be a Set, was: class java.util.ArrayList; nested e

在SpringDataNeo44中,我们只有
repository.save(entity)
,但例如,当我的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();
}

在您的
UserEntity
域对象中,您是否存储了任何关系?确保它们声明为
Set
,而不是
Iterable

发件人:

“也可以有引用一组节点的字段 实体(1:N)。这些字段有两种形式,可修改或 只读。可修改字段的类型为Set,且为只读 字段是可编辑的,其中T是一个@NodeEntity注释类。”


我怀疑您的默认构造函数正在实例化ArrayList…

因为您使用的是SDN,所以不需要手动启动/提交任何事务

假设您的
User
类如下所示

@NodeEntity(label="User)
public class User extends DomainObject{
    @Property(name = "email")
    private String email;

    //getter and setter
}
您的
UserRepository
与此类似:

public interface UserRepository extends GraphRepository<User> {

    //maybe this is already right at hand by SDN and thus redundant?
    @Query("MATCH (u:User {email:{email}}")
    public User findByEmail(@Param("email") String email)
}

您的意思是,若实体具有图形id,那个么save还可以更新该实体?您的意思是,当传递图形id时,save还可以更新哪个实体?实际上Spring接受事务,我的Springbean是事务性的,并且成功保存。我不理解你的问题(我在Standalone模式下使用Spring-data-neo4j,所以没有Spring框架)。不管怎样,你已经看过了吗?有使用spring-data-neo4jYes的例子,我读了一篇文档,但我的问题是您在哪里已经创建了实体和关系,当用户更改其电子邮件(即实体属性)时,我如何更新@Node entity。保存操作失败。NodeEntity公共类UserEntity实现可序列化的{private static final long serialVersionUID=1L;public static final String FRIEND=“FRIEND”;GraphId private long id;index(unique=true)private long userId;private String email;Fetch RelatedTo(elementClass=UserEntity.class,type=FRIEND,direction=direction.outing)私有列表好友列表;我只有@Fetch@RelatedTo(elementClass=UserEntity.class,type=FRIEND,direction=direction.outing)私有列表好友列表;是的,这是您的问题。列表在关系上下文中没有意义,因为它们在图形中总是无序的。请将列表替换为Set:
@Fetch@RelatedTo(elementClass=UserEntity.class,type=FRIEND,direction=direction.outing)private Set friendsList;
您好,谢谢,我现在将列表替换为Set and all ok,就在前端,我将数据从Set填充到ArrayList,因为我需要iterable类型。谢谢您也请共享您的
用户
对象的定义?
@Component
@Transactional
public class UserService {

    @Autowired
    private UserRepository userRepository;

    public void updateEmail(String email) {
        User user = userRepository.findByEmail(email);
        if (user == null) return; //or throw...
        user.setEmail(email);
        userRepository.save(user);
    }
}