kenlz
kenlz

Reputation: 461

Neo4J SDN 4 Unable to save

this is my configuration

@EnableTransactionManagement
@EnableScheduling
@EnableAutoConfiguration
@ComponentScan(basePackages = {"id.co.babe.neo4j.service"})
@Configuration

public class MyNeo4jConfiguration extends Neo4jConfiguration {
  private static final Logger LOGGER = LoggerFactory.getLogger(MyNeo4jConfiguration.class);

  @Value("${neo4j.server.user}")
  private String user;
  @Value("${neo4j.server.pass}")
  private String pass;
  @Value("${neo4j.server.host}")
  private String host;

 @Override
 public Neo4jServer neo4jServer() {
     return new RemoteServer(host,user,pass);
 }

 @Override
 public SessionFactory getSessionFactory() {
     return new SessionFactory("app.neo4j.domain");
 }

 @Bean
 @Primary
 public Neo4jOperations getNeo4jTemplate() throws Exception {
     return new Neo4jTemplate(getSession());
 }

and this is my domain User

@NodeEntity
public class User{
  @GraphId
  private Long Id;
  private String name;
  private int age;
  private String country;

and my service interface

public interface UserService { 
  public User create(User user);
  public User read(User user);
  public List<User> readAll();
  public User update(User user);
  public Boolean delete(User user);
}

and my implementation

@Service
@Transactional
public class UserServiceImpl implements UserService{

  @Autowired
  Neo4jOperations template;

  @Override
  public User create(User user){
    return template.save(user);
  }

and this is my main class

for(int i = 0; i < 10; i++){
  app.neo4j.domain.User user = new app.neo4j.domain.User();
  user.setAge(13);
  user.setCountry("Philly");
  user.setId(i);
  user.setName("Ibanez" + i);
  LOGGER.info("Inserting {}",user.getName());
  service.create(user);
}

no error was found, but when I go to neo4j console (localhost:7474), and run this query match(n) return n, which should return all nodes in the database. unfortunately there was no nodes found even though i was able to save without errors. I wonder what's wrong.

I also tried doing it with @enablingNeo4jRepositories with no difference to the result.

Upvotes: 0

Views: 49

Answers (1)

Luanne
Luanne

Reputation: 19373

Your code should never set the value of the @GraphId field. This field is used internally to attach entities to the graph.

If you remove user.setId(i);, your entities should be saved correctly.

Note that you can add your own custom ID field, but you still need another field for the GraphID e.g.

@GraphId private Long graphId; //used internally, never assign a value
private Long id; //your application id, stored as a property on the entity

Upvotes: 4

Related Questions