Spring Data Neo4j 不插入新节点,只更新具有相同属性的现有节点
Spring Data Neo4j does not insert a new node, only updates an existing one that has the same properties
当我尝试为 SpringData 保存具有与现有节点相同的属性和关系的新节点时,它只会更新现有节点,而不会插入新节点。我正在用空 ID 保存它。
有什么问题?
Neo4j 3.0.0
Spring 数据 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 用户扩展模型对象{
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 ;
}
这可能是因为您明确将 id 设置为 null。 OGM 会话跟踪实体引用,这种情况是无效的 - 一个已知的、以前保存的实体,现在 ID 为空。为什么不创建一个新的 Collection 对象来保存?
根据评论更新
SDN/OGM 只会在具有相同属性集的两个给定节点之间创建一个关系。在一对节点之间建立具有相同 属性 值的关系通常没有太大价值。如果您的图形模型需要的话,按照您的描述添加时间戳是强制建立多个关系的一种方法。
当我尝试为 SpringData 保存具有与现有节点相同的属性和关系的新节点时,它只会更新现有节点,而不会插入新节点。我正在用空 ID 保存它。
有什么问题?
Neo4j 3.0.0
Spring 数据 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 用户扩展模型对象{
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 ;
}
这可能是因为您明确将 id 设置为 null。 OGM 会话跟踪实体引用,这种情况是无效的 - 一个已知的、以前保存的实体,现在 ID 为空。为什么不创建一个新的 Collection 对象来保存?
根据评论更新
SDN/OGM 只会在具有相同属性集的两个给定节点之间创建一个关系。在一对节点之间建立具有相同 属性 值的关系通常没有太大价值。如果您的图形模型需要的话,按照您的描述添加时间戳是强制建立多个关系的一种方法。