简体   繁体   English

Spring Data Neo4j在一个NodeEntity中合并不同的类

[英]Spring Data Neo4j merge different classes in one NodeEntity

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. 我目前正在使用spring-data-neo4j(当前版本)开发一个项目,并且在尝试将两个类及其属性合并到一个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. 通过定义用@Index注释的id属性,SDN可以进行合并,而不是插入具有相同索引的多个节点。

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. 我想要实现的是,如果我保存一个Ent1实例,然后再保存另一个与Ent1实体具有相同ID的Ent2实例,则这两个数据属性都应该出现在结果节点中。 They should be merged. 它们应该合并。

In fact either data1 or data2 is present, dependent on which entity has been saved last. 实际上,取决于最后保存的实体,存在data1或data2。 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? 有没有人有合并所有属性字段而不是替换/删除它们的解决方案?

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. 不幸的是,SDN本身并未将此类查询用于实体合并。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM