# Creating Relationships to Existing Nodes on Save

**URL:** <https://community.neo4j.com/t/creating-relationships-to-existing-nodes-on-save/7941>\
**Category:** Spring Data Neo4j & Neo4j-OGM\
**Created:** [June 9, 2019, 6:36pm UTC](https://community.neo4j.com/t/creating-relationships-to-existing-nodes-on-save/7941 "2019-06-09T18:36:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ethan.hohensee](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ethan.hohensee/32/5156_2.png) [@ethan.hohensee](https://community.neo4j.com/u/ethan.hohensee)\
**Post date:** [June 9, 2019, 6:36pm UTC](https://community.neo4j.com/t/creating-relationships-to-existing-nodes-on-save/7941/1 "2019-06-09T18:36:43Z")

</div>

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.

```auto
@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

```auto
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.

---

<div class="post-metadata">

**Author:** ![ethan.hohensee](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/ethan.hohensee/32/5156_2.png) [@ethan.hohensee](https://community.neo4j.com/u/ethan.hohensee)\
**Post date:** [June 10, 2019, 1:48pm UTC](https://community.neo4j.com/t/creating-relationships-to-existing-nodes-on-save/7941/2 "2019-06-10T13:48:03Z")

</div>

I was able to figure this out by looking for information on Spring Data REST and JPA, not specifically for Neo4J. This [article](https://www.baeldung.com/spring-data-rest-relationships) details the method. A link to the object you want to create the relationship with is added as the value to the JSON.

```auto
{
  "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.

---

<div class="post-metadata">

**Author:** ![benjaminh](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/benjaminh/32/5658_2.png) [@benjaminh](https://community.neo4j.com/u/benjaminh)\
**Post date:** [July 16, 2019, 9:30am UTC](https://community.neo4j.com/t/creating-relationships-to-existing-nodes-on-save/7941/3 "2019-07-16T09:30:06Z")

</div>

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](http://localhost:8080/files) and [http://localhost:8080/assets](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](http://localhost:8080/files) with the following body

```auto
{
    "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 ?
