cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Creating Relationships to Existing Nodes on Save

I'm trying to understand the canonical way to save relationships to existing nodes with Spring Data Neo4j and Spring Data REST. I have my domains model mapped out into entity classes and SDN repositories. I can successfully create a user or a campaign with simple properties listed, but when it comes time to create a new Campaign and tie it to an existing user, I'm not sure what to do in the JSON of the the request.

@Getter
@Setter
public abstract class Entity {

    @Id
    @GeneratedValue(strategy = UuidStrategy.class)
    @Convert(UuidConverter.class)
    private UUID eid;
}

@Getter
@Setter
@NodeEntity
public class User extends Entity {

    @Required
    @Index(unique = true)
    private String username;

    @Required
    @Index(unique = true)
    private String email;

    @Relationship("CAMPAIGN_OWNER")
    private List<Campaign> campaignsOwned;
}

@Getter
@Setter
@NodeEntity
public class Campaign extends Entity {

    @Required
    private String name;

    @Required
    @Relationship(type = "CAMPAIGN_OWNER", direction = Relationship.INCOMING)
    private User owner;
}

And the repository interfaces

public interface UsersRepository extends Neo4jRepository<User, UUID> {
    User findByUsername(@Param("username") String username);
    User findByEmail(@Param("email") String email);
}


public interface CampaignsRepository extends Neo4jRepository<Campaign, UUID> { 
}

Any help would be greatly appreciated. I've been searching the reference doc for Spring Data Neo4j and StackOverflow for a hours now and feel like I'm missing something.

2 REPLIES 2

I was able to figure this out by looking for information on Spring Data REST and JPA, not specifically for Neo4J. This article details the method. A link to the object you want to create the relationship with is added as the value to the JSON.

{
  "name": "A New Campaign",
  "owner": "http://localhost:8080/api/v1/users/9b5f6c15-7f62-4747-b14c-44d3e11c0f2c"
}

This allows Spring Data Neo4j to find the entity by its ID and create the relationship.

Hello there, I'm facing a similar issue and did not manage to succeed following your advice.

I have a File entity with a native id, an uuid, and several properties, and an Asset entity with a native id and a string id. Both are exposed through the default endpoints http://localhost:8080/files and http://localhost:8080/assets

The point is I already have Asset nodes in my graph, and I want to create File nodes and their relationship to corresponding assets at the same time.
I tried doing a POST request to http://localhost:8080/files with the following body

{
    "label":"default",
    "md5":"e80f000f56884ef666",
    "mimetype":"image/jpeg",
    "path":"path/test.jpg",
    "size":"150000",
    "asset": "http://localhost:8080/assets/my_string_id"
}

The files are created but not the relationships. In your case, your users IDs are API URIs or uuids ? I do not use the resource URI of the asset as the ID so that may be the problem. However, I tried changing the URI by only my_string_id and also the native Neo4j ID but that does not solve the problem.

Any ideas ?