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

时间:2013-03-18 12:54:27

标签: java neo4j spring-data-neo4j

在Spring数据 neo44 中我们只有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 
  }

3 个答案:

答案 0 :(得分:3)

您必须在事务中嵌入.save()。

举个例子:

final org.neo4j.graphdb.Transaction tx = this.neoTemplate.getGraphDatabaseService().beginTx();
try {
    updatedUser = userRepository.save(updatedUser);
    tx.success();
} finally {
    tx.finish();
}

答案 1 :(得分:2)

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

  

来自:http://static.springsource.org/spring-data/data-graph/snapshot-site/reference/html/#reference:programming_model:relationships:relatedto

     

“也可以有引用一组节点的字段   实体(1:N)。这些字段有两种形式,可修改或   只读。可修改字段的类型为Set,并且是只读的   fields是Iterable,其中T是@NodeEntity-annotated class。“

我怀疑你的默认构造函数是在实例化一个ArrayList ...

答案 2 :(得分:0)

由于您正在使用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)
}

然后,您可以在@Transactional课程中使用UserService

@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);
    }
}