How can I update a node or node object in spring neo4j data?

In spring neo44 data, we only have repository.save(entity) , but, for example, when the UserEntity (email) property has changed, I don’t know how to update it.

I also tried using the neo4j template, but saving the object with an existing node id caused a rollback below.

 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) 

How can we update node or nodeentity?

 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
source share
3 answers

You must embed .save () in the transaction.

As an example:

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

Are there any relationships in your UserEntity domain object? Make sure they are declared as Set<T> and not as Iterable<T> :

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

"It is also possible to have fields that refer to a set of node entities (1: N). These fields are of two types: modifiable or read-only. Modifiable fields are of type Set and are read-only of the Iterable field, where T is the class @ NodeEntity-annotated . "

I suspect your default constructor is instantiating an ArrayList ...

+2
source

Since you are using an SDN, you will never have to manually start / complete transactions.

Suppose your User class looks like this:

 @NodeEntity(label="User) public class User extends DomainObject{ @Property(name = "email") private String email; //getter and setter } 

and your UserRepository is like this:

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

Then you can use @Transactional in the UserService class:

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

All Articles