0
votes

I'm currently working on a project with spring-data-neo4j (current release) and I'm facing a problem when trying to merge two classes and their properties into one NodeEntity. Here are my two classes:

@NodeEntity(label = "entity")
public class Ent1 {
    @Id
    @Index(unique = true)
    private Integer id;

    private Integer data1;
}

.

@NodeEntity(label = "entity")
public class Ent2 {
    @Id
    @Index(unique = true)
    private Integer id;

    private Integer data2;
}

By defining the id property annotated with @Index, SDN is doing merges instead of inserting multiple nodes with the same index.

What I want to achieve is that if I save an instance of Ent1 and afterwards another instance of Ent2 with the same id as the Ent1 entity both data attributes should be present in the resulting node. They should be merged.

In fact either data1 or data2 is present, dependent on which entity has been saved last. Merge doesn't really seem to merge, instead it replaces the entities properties.

Does anyone have a solution for merging all property fields instead of replacing/removing them?

1

1 Answers

1
votes

I just solved my problem with a custom cypher query. If someone is also facing a problem like this, here is the solution for the problem abstraction above.

@Query("MERGE (e:entity{id:{ent1}.id}) SET e.data1 = {ent1}.data1"
void saveEnt1(@Param("ent1") Ent1 ent1);

@Query("MERGE (e:entity{id:{ent2}.id}) SET e.data2 = {ent2}.data2"
void saveEnt2(@Param("ent2") Ent2 ent2);

Unfortunately SDN does not use such a query natively for an entity merge.