1
votes

When I try to save a new node for the SpringData, with the same properties and relationships of an existing node, it just updates the existing and does not insert the new node. I'm saving it with the null ID.

What's the problem?

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")

public class User extends 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 ;

}

1
Hard to say without seeing some codeLuanne
I've edited the post. Sorry for the code formatting. Basically the method that this saving is addItemCollection (Collection collection, Long itemId). Basically it calls the save method GraphRepository <Collection>. Thank's!Deyvid Pinzon

1 Answers

1
votes

This is probably because you explicitly set the id to null. The OGM session tracks entity references and this case is invalid- a known, previously saved entity with a null id now. Why would you not create a new Collection object to save?

Updated based on comments

SDN/OGM will only create one relationship between two given nodes with the same set of properties. There is usually not much value in having relationships with identical property values between a pair of nodes. Adding a timestamp as you described is one way to force multiple relationships if that is what your graph model needs.