Reputation: 67
I've got something weird in my project and I can't see if it's may fault or not.
I'm using Spring with spring-data-neo4j (v4.0.0).
This code in my controller breaks relationships.
@RequestMapping("/initialize")
public Object initialize() {
Organization orga = new Organization();
orga.setName("WEAVERS");
this.organizationRepository.save(orga);
User user = new User();
user.setEmail("[email protected]");
user.setFirst_name("Thierno");
user.setLast_name("Rignoux");
user.setLogin("317");
user.setOrganization(orga);
this.userRepository.save(user);
User user2 = new User();
user2.setEmail("[email protected]");
user2.setFirst_name("Eléonore");
user2.setLast_name("Klein");
user2.setLogin("nessie");
user2.setOrganization(orga);
this.userRepository.save(user2);
Project project = new Project();
project.setName("PROJET 1");
project.setOrganization(orga);
project.setUser(user);
this.projectRepository.save(project);
Project project2 = new Project();
project2.setName("PROJET 2");
project2.setOrganization(orga);
project2.setUser(user2);
this.projectRepository.save(project2);
return orga;
}
As you can see on this graph:
The relation between ORGANIZATION and USER1 has been severed when I created USER2.
Broken relationships is something I see everywhere in my application... I don't understand!
neo4j configuration
@EnableTransactionManagement
@Import(RepositoryRestMvcConfiguration.class)
@EnableScheduling
@EnableAutoConfiguration
@ComponentScan(basePackages = {"fr.weavers.loom"})
@Configuration
@EnableNeo4jRepositories(basePackages = "fr.weavers.loom.repositories")
public class LoomNeo4jConfiguration extends Neo4jConfiguration {
public static final String URL = System.getenv("NEO4J_URL") != null ? System.getenv("NEO4J_URL") : "http://localhost:7474";
@Override
public Neo4jServer neo4jServer() {
return new RemoteServer(URL,"neo4j","loomREST2016");
}
@Override
public SessionFactory getSessionFactory() {
return new SessionFactory("fr.weavers.loom.domain");
}
}
Organization Domain :
public class Organization extends Node {
@GraphId
Long id;
String name;
@Relationship(type = "SET_UP", direction = Relationship.OUTGOING)
Set<SET_UP> projects;
@Relationship(type = "WORK_FOR", direction = Relationship.INCOMING)
Set<WORK_FOR> users;
[...]
WORK_FOR Domain :
public class WORK_FOR extends Relationship {
@GraphId
private Long relationshipId;
@StartNode
public User user;
@EndNode
public Organization organization;
public WORK_FOR() {
super();
}
}
User Domain:
public class User extends Node {
@GraphId
Long id;
String login;
@JsonIgnore
String password;
String first_name;
String last_name;
String email;
@Relationship(type = "WORK_FOR")
WORK_FOR workFor;
I looked everywhere and I can't find anything to help me...
Thank you
Upvotes: 1
Views: 171
Reputation: 2181
In your initialisation request you make several repository calls to construct the database. Each of these calls will construct a new Session
, effectively clearing out information from the previous one because the Spring config defaults to Session-per-request. This is most likely the reason existing relationships are being deleted.
If you add the following bean definition to your config, I believe the problem should be resolved.
@Override
@Bean
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public Session getSession() throws Exception {
return super.getSession();
}
Please refer to the documentation here for more information about Session scope.
Upvotes: 1
Reputation: 2181
Please annotate the incoming relationship on your Node class:
@Relationship(type = "WORK_FOR", Direction = Relationship.INCOMING)
WORK_FOR workFor;
As per the documentation, all INCOMING relationships must be fully annotated - the default is OUTGOING.
Upvotes: 0