4

在 Spring data neo44中,我们刚刚repository.save(entity),但例如当我的 UserEntity 的属性(电子邮件)发生变化时,我不知道如何更新它。

我也尝试使用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 
  }
4

3 回答 3

3

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

举个例子:

final org.neo4j.graphdb.Transaction tx = this.neoTemplate.getGraphDatabaseService().beginTx();
try {
    updatedUser = userRepository.save(updatedUser);
    tx.success();
} finally {
    tx.finish();
}
于 2013-03-18T13:22:14.830 回答
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类型,只读字段是Iterable,其中 T 是 @NodeEntity 注释的类。”

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

于 2013-03-19T16:15:47.313 回答
1

由于您使用的是 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类上使用 a :

@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);
    }
}
于 2016-10-23T20:09:40.947 回答