5

I need to create a new node of the class A, which has a relationship with the User node:

@NodeEntity
public class A implements Serializable {

    /**
     * Graph node identifier
     */
    @GraphId
    private Long nodeId;

    /**
     * Enity identifier
     */
    private String id;

    //... more properties ...

    @Relationship(type = "HAS_ADVERTISER", direction = Relationship.OUTGOING)
    private User user;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof IdentifiableEntity)) return false;

        IdentifiableEntity entity = (IdentifiableEntity) o;
        if (!super.equals(o)) return false;

        if (id != null) {
            if (!id.equals(entity.id)) return false;
        } else {
            if (entity.id != null) return false;
        }

        return true;
    }


}


@NodeEntity
public class User implements Serializable {

    /**
     * Graph node identifier
     */
    @GraphId
    private Long nodeId;

    /**
     * Enity identifier
     */
    private String id;

    //... more properties ...


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof IdentifiableEntity)) return false;

        IdentifiableEntity entity = (IdentifiableEntity) o;
        if (!super.equals(o)) return false;

        if (id != null) {
            if (!id.equals(entity.id)) return false;
        } else {
            if (entity.id != null) return false;
        }

        return true;
    }

}

Now, let's suppose we have the following data for the new node A:

{
  "id": 1,
  "nodeId": "0001-0001",
  "user": {
           "id": 4,
           "nodeId": "0002-0002",
           "name": null,
           "firstName": null
          }
}

I'm trying to create the node A with a relationship between the new node A and the (already existing) node of the User who has the "id":4 and "nodeId":"0002-0002" (unique node identifier), but instead of that, the User node updates the fields "name" and "firstName" with to null.

I'm using the GraphRepository proxy to create it:

@Repository
public interface ARepository extends GraphRepository<A> {


}

Is there any way to do it without this update, only making the relationship with the User node?

troig
  • 7,072
  • 4
  • 37
  • 63
Xavi Torrens
  • 337
  • 1
  • 12

2 Answers2

2

No, you'll have to either reload the entity by id to populate all missing values and then save, or write a custom query.

Luanne
  • 19,145
  • 1
  • 39
  • 51
1

You can do it with a MERGE. See the Cypher RefCard

@Repository
public interface ARepository extends GraphRepository<A> {

    @Query("MERGE (a:A {id:aId}})-[:HAS_ADVERTISER]-(u:User {id:{userId}})" +
           "RETURN a")
    public A createAndGetAd(@Param("aId") String aId,
                            @Param("userId") String userId)
    }
Christoph Möbius
  • 1,352
  • 1
  • 12
  • 18