【问题标题】:Spring Data Neo4j does not insert a new node, only updates an existing one that has the same propertiesSpring Data Neo4j 不会插入新节点,只会更新具有相同属性的现有节点
【发布时间】:2017-01-03 08:11:37
【问题描述】:

当我尝试为 SpringData 保存一个具有与现有节点相同的属性和关系的新节点时,它只是更新现有节点而不插入新节点。我用空 ID 保存它。

有什么问题?

Neo4j 3.0.0
Spring Data 4.1.2
Neo4j OGM 2.0.2

 public abstract class ModelObject {

    @GraphId
    protected Long id;

    //getters and setters

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || id == null || getClass() != o.getClass())
            return false;

        ModelObject entity = (ModelObject) o;

        if (!id.equals(entity.id))
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        return (id == null) ? -1 : id.hashCode();
    }

}


    @RelationshipEntity(type = "COLLECTION")
public class Collection  extends ModelObject{

    @DateString("yyyy-MM-dd")
    private Date acquisitionDate;
    @StartNode
    private User collector;
    @EndNode
    private Item item;
    private Boolean manual;
    private Boolean box;
    private Double paidValue;
    private String historyAcquisition;

    //getters and setters

}



 @Service
public class CollectionServiceImpl implements ICollectionService {

    @Autowired
    private UserRepo userRepo;

    @Autowired
    private CollectionRepo collectionRepo;

    @Autowired
    private ItemRepo itemRepo;

    @Override
    public Iterable<Collection> findByUserId(Integer idUser) {
        return collectionRepo.findByCollectorId(idUser);
    }

    @Override
    public boolean addItemCollection(Collection collection, Long itemId) {

        try {

        Long userId = collection.getCollector().getId();

        collection.setCollector(userRepo.findOne(userId, 1));
        collection.setItem(itemRepo.findOne(itemId, 1));
        collection.setId(null);


        collectionRepo.save(collection);

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }


    @Override
    public boolean removeItemCollection(Long collectionId, Long itemId) {

        try {

        collectionRepo.delete(collectionId);

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }


}


@NodeEntity(label="USER")

公共类用户扩展 ModelObject{

private String fullName;
private String userName;
private String password;
private Country country;

@DateString(DateUtil.yyyy_MM_dd)
private Date birthDate;

@Relationship(type="FOLLOWING", direction=Relationship.OUTGOING )
private Set<Following> following;

@Relationship(type="COLLECTION", direction=Relationship.OUTGOING )
private List<Collection> collection ;

}

【问题讨论】:

  • 不看一些代码很难说
  • 我已经编辑了帖子。对不起,代码格式。基本上这种保存的方法是addItemCollection(Collection collection, Long itemId)。基本上它调用保存方法 GraphRepository 。谢谢!

标签: java neo4j spring-data spring-data-neo4j


【解决方案1】:

这可能是因为您将 id 显式设置为 null。 OGM 会话跟踪实体引用,这种情况是无效的——一个已知的、以前保存的、现在具有空 id 的实体。为什么不创建一个新的 Collection 对象来保存?

根据 cmets 更新

SDN/OGM 只会在具有相同属性集的两个给定节点之间创建一种关系。在一对节点之间建立具有相同属性值的关系通常没有多大价值。如您所述,添加时间戳是强制多个关系的一种方法,如果这是您的图形模型需要的话。

【讨论】:

  • 你好 Luanne,其实我没有解释清楚。 “collection”对象是在调用此方法之前创建的,那么它就是一个新对象。之前我没有为 id 设置 null 值,这是我最后一次尝试。
  • 那么它应该可以工作了——你能给我们发一个测试用例吗? github.com/neo4j/neo4j-ogm/issues
  • 好吧,我做了什么作为临时解决方案。我向对象添加了另一个属性,即“@DateLong private Date registrationDate”。始终插入当前时间,因此它不会重复先前持久对象的相同属性值。但我还是想知道更新的原因,不插入节点。
  • 更新了我的答案,您的评论有助于更好地解释问题
  • 嗨 Luanne... 我更新了我的问题。我添加了我的“用户”类,认为“集合”列表中的映射会是问题所在。 @Relationship (Type = "COLLECTION" 方向 =Relationship.OUTGOING) 私有列表 集合;但随之而来的问题是,即使要复制一个时间戳属性。谢谢你的帮助! =D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-29
  • 1970-01-01
相关资源
最近更新 更多