spring-projects / spring-data-neo4j

Provide support to increase developer productivity in Java when using Neo4j. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
http://spring.io/projects/spring-data-neo4j
Apache License 2.0
834 stars 618 forks source link

Unable to create a relationship with save method of Neo4jRepository #2731

Closed RosarioB closed 1 year ago

RosarioB commented 1 year ago

Hello, I have experienced an issue when trying to create a relationship with Spring version 3.0.1.

These are the entities:

@Node("Movie")
@Getter @Setter  @AllArgsConstructor @ToString
public class Movie {

    @Id
    @GeneratedValue
    //@JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private Long id;

    @Property("title")
    private String title;

    @Property("description")
    private String description;

    @Relationship(type = "ACTED_IN", direction = Relationship.Direction.INCOMING)
    private List<Roles actorsAndRoles;

    @Relationship(type = "DIRECTED", direction = Relationship.Direction.INCOMING)
    private List<Person directors;
}

 @Node("Person")
 @Getter @Setter @AllArgsConstructor @ToString
 public class Person {

     @Id @GeneratedValue
     private Long id;

     @Property("name")
     private String name;

     @Property("born")
     private String born;

 }

 @Getter @Setter @AllArgsConstructor @ToString
 @RelationshipProperties
 public class Roles{

     @RelationshipId
     private Long id;

     @TargetNode
     private Person person;

     @Property("roles")
     private List<String roles;
 }

This is the controller (the class is called MovieController) method I am calling:

@PostMapping
    public ResponseEntity create(@RequestBody Movie movie){
        try {
            movie.setId(null);
            return ResponseEntity.ok(movieService.save(movie));
        }
        catch(Exception e){
            return ResponseEntity.internalServerError().body(e.getMessage());
        }
    }

I have uploaded the code here: https://github.com/RosarioB/spring-boot-crud-rest-api-neo4j/tree/basic_crud

This is a body example of a POST request at: http://localhost:8080/api/movies

{

   "title":"Matrix",
   "description":"Science fiction",
   "actorsAndRoles":[
       {
           "person":{
               "name":"Keanu Reeves",
               "born":"27-01-1963"
           }
       }
   ]

}

And this is the response:

{
    "id": 16,
    "title": "Matrix",
    "description": "Science fiction",
    "actorsAndRoles": [
        {
            "id": null,
            "person": {
                "id": 17,
                "name": "Keanu Reeves",
                "born": "27-01-1963"
            },
            "roles": null
        }
    ],
    "directors": null
}

On the database only the 2 nodes (one Person and one Movie) got created but not the relationship. I would have expected the relationship ACTED_IN to be created too.

By downgrading to Spring version 3.0.6 the relationship got created.

meistermeier commented 1 year ago

Thanks for reporting this. The good thing is, that this is a known bug in SDN 7.1.0 that got already fixed in https://github.com/spring-projects/spring-data-neo4j/issues/2728 If you want, you could try the 7.1.1-SNAPSHOT or wait until a patch got released. Closing this now, but feel free to comment if it does not solve your problem.