2016-08-26 50 views
1

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

有什么问题?

的Neo4j 3.0.0
春数据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 ; 

}

+0

很难说没有看到一些代码 – Luanne

+0

我已编辑帖子。对不起,代码格式。 基本上这个保存方法是addItemCollection(Collection collection,Long itemId)。基本上它调用保存方法GraphRepository 。 谢谢! –

回答

1

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

更新基于意见

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

+0

你好Luanne, 其实我没有解释得很对。调用此方法之前创建“collection”对象,然后它是一个新对象。更早的时候,我没有为id设置null值,这是我做出的最后一次尝试。 –

+0

然后它应该可以工作 - 可以给我们一个测试用例吗? https://github.com/neo4j/neo4j-ogm/issues – Luanne

+0

那么,我做了什么临时解决方案。我向对象添加了另一个属性,即“@DateLong私人日期注册日期”。始终使用当前时间插入,因此它不会重复先前持久对象的相同属性值。但我仍然想知道更新的原因,而不是插入节点。 –

相关问题