New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SDN4: Unable to persist rich relationship [DATAGRAPH-763] #1330
Comments
Vince Bickers commented It is important to ensure that rich relationships are wired up correctly. In the current entity->graph mapping logic, the OGM does not directly save relationships. Instead, when you try to persist a rich relationship object the OGM will select the rich relations's If your wire up the source and target objects so that they maintain a reference to the rich relationship, I think it should solve the problem |
Eric Spiegelberg commented If I understand you correctly, we need to add a reference to the rich relationship as a property of both the start and end nodes. We’ve done just that in the updated attached code. When performing a test to create and persist the entities and relationship, the first time the code is executed everything and the relationship is correctly peristed. However, executing the same controller endpoint the the relationship itself is deleted. Performing the operation multiple times demonstrates that the relationship is created on the first execution, deleted on the subsequent execution, created on the third execution, and so on. To reproduce what we’re seeing:
Can you help us understand why the relationship is deleted? We would have expected it to not be. The approach you’ve outlined — of adding a reference to the rich relationship as a property of both the start and end nodes — does not appear to be documented in the SDN4 reference information. It is also not implemented in either the sdn4-university (which has no rich relationships) or sdn4-cineasts projects. Knowing that 4.0.GA is a total rewrite, is the need for this approach a departure from the functional behavior of Spring Data Neo4j? |
Eric Spiegelberg commented Updated sample code to demonstrate Vince's approach of adding a reference to the start and end nodes |
Luanne Misquitta commented The references look fine now, but the relationship entity has no properties and hence it is an incorrect relationship entity (and you should use a standard private long since; and then always set it so that the relationships are distinguishable teacherAndStudentRelationship.setSince(System.currentTimeMillis()); the code works correctly. Agreed that the documentation is lacking in this respect, we'll make sure to have this taken care of before the next release. BTW the sdn4-cineasts project does contain a relationship entity- see https://github.com/neo4j-examples/sdn4-cineasts/blob/master/src/main/java/org/neo4j/cineasts/domain/Actor.java#L33 |
Eric Spiegelberg commented I've digested the information Luaane provided in updating https://jira.spring.io/browse/DATAGRAPH-763. In doing so, I updated my sample sdn4-university sample project attached to DATAGRAPH-763 by:
public TeacherAndStudentRelationship(Teacher teacher, Student student) {
this.teacher = teacher;
this.student = student;
this.teacher.getStudents().add(student);
this.student.getTeachers().add(teacher);
}
TeacherAndStudentRelationship teacherAndStudentRelationship = new TeacherAndStudentRelationship(teacher, student);
teacherAndStudentRelationship.setDate(new Date());
teacherAndStudentRelationship.setName("name");
teacherService.createOrUpdate(teacher); In short, the above changes now mirror the usage of Role in sdn4-cinecasts as well as Luanne's blog post in http://graphaware.com/neo4j/2015/09/03/sdn-4-object-model.html and the behavior of my sample now works as expected and the relationship, along with it's properties, work properly. Taking a big step back and suspending any judgement of good or bad, from my view this is a pretty big design and usage change in how rich relationships work in SDN 4 versus SDN 3. Is that a fair statement to make? The fact that while using SDN 3 we were able to make use of "anaemic domain models" (as Luanne referred to them in her blog post) without issue but are now experiencing issues after upgrading to SDN 4 leads me to conclude that, again with out judgment of good or bad, the change in how relationships are designed/used in SDN 4 is a big change. Either way, on behalf of the SDN community -- particularly those that have used rich relationships in SDN 3 -- I think it's pretty important that the (new?) SDN4 design and usage of rich relationships get more coverage in the documentation as well as the SDN 4 example/sample projects. That said, we wish to move our applicaiton forward in alignment with SDN4 best practices. In our current design we have a fairly small number of entities (a dozen or two) that can participate in what may be on the order of a dozen or two relationships. Should the information in Luanne's blog post be considered the recommend modeling approach of relationships -- both simple and rich -- in SDN4? That is to say, would you recommend we revise our domain model so that entities contain a collection for each relationship it may participate in? This isn't necessarily a bad thing, it's just that we were excited and enjoyed the SDN 3 approach where our entities were largely containers of data without the clutter of relationship collections and their associated getter/setters (I believe that this is what Luanne refers to as "anaemic" domain models in her post). It's not the end of the world if we have to overhaul our domain, we simply want to adopt the recommend approach |
Eric Spiegelberg commented For the benefit of the community, those that find this item in the future, and to formally close this thread off, it is accurate to say that this issue is working as expected. After having some deeper offline conversations with GraphAware:
|
Eric Spiegelberg opened DATAGRAPH-763 and commented
Having upgraded our application from Spring Data Neo4j (SDN) 3.x to SDN 4.0.GA, our application is no longer able to persist rich relationships. Trying to isolate the problem and learn what we’re doing wrong on our end, we have modified the sdn4-unversity sample project to mirror our approach and, to our surprise, are seeing the same issue.
The attached sdn4-unversity has had a rich relationship, named TeacherAndStudentRelationship and annotated with
@RelationshipEntity
, and its associated repository and service added. The TeacherController has been modified to add a method that demonstrates the issue.To demonstrate that entities (ie: nodes) are being persisted correctly:
With an empty database, start Neo4j (we’re using 2.2.2 and 2.2.5)
Download, extract, enter, and start the attached sdn4-university with the following command line:
a. mvn clean test spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005 -Dusername=[YOUR_NEO4J_USERNAME] -Dpassword=[YOUR_NEO4J_PASSWORD]"
Attach your remote debugger, such as through Eclipse
Set a debug breakpoint on GenericService, line 32, where the entity is about to be save
Using a REST tool, such as Chrome’s Advanced REST Client, submit a “application/json” POST request to http://localhost.capella.edu:8080/api/teachers with the following JSON payload: {"name": "Demo Teacher"}
When your debugger catches on GenericService, line 32, notice that the id of the new Teacher is null
Step over the save operation on line 32
Notice that the id of the new Teacher is now populated with a Long value, indicating it has been persisted to Neo4j
Let your debugger run, fully completing the request
Use the Neo4j console to validate the teacher was persisted, validating the teacher with the name “Demo Teacher” exists
Notice that “Teacher” is correctly listed within Neo4j’s “Node Labels” section
Populate the sample reference data by using a web browser to hit: http://localhost:8080/api/reload
Now, to reproduce the relationship issue we are seeing:
Use a web browser to hit: http://localhost:8080/api/teachers/relate/11/33 . This invokes our customized code in TeacherController that instantiates and persists our rich relationship class
When your debugger catches on GenericService, line 32, notice that the id of the new TeacherAndStudentRelationship is currently null, as expected
Step over the save operation on line 32
Notice that the id of the new TeacherAndStudentRelationship is still null, indicating it has not been correctly persisted
Using the Neo4j console, notice that TeacherAndStudentRelationship is not listed in the “Relationship Types” section
sdn4-unveristy, as it currently sits in github, is making use of spring-boot 1.3.0.M1. We have upgraded our sd4n-university to make use of spring-boot 1.3.0.M5 (the latest), retested, and unfortunately have the same incorrect results.
The ability to persist rich relationships is obviously an important feature of SDN/Neo4j. We expected to make these modifications to sdn4-unversity to demonstrate the problem was on our end in our application, yet were surprised to be able to seemingly reproduce the issue. We’ll be thrilled to find out the problem is something we’re doing wrong, if someone can help us out and show us what it is. Having raised the issue, we will continue to investigate but would greatly appreciate a second set of eyes from the community.
Along with the above reference to my sdn4-university github fork demonstrating my code, attached is the src directory of my changes (src directory only to stay under the 10meg upload limit).
Affects: 4.0 GA
Reference URL: https://github.com/espiegelberg/sdn4-university
Attachments:
The text was updated successfully, but these errors were encountered: