[英]spring-data-neo4j remove nodeEntity and all referenced nodes
I've got a simple graph model: 1 User
has N SocialUser
. 我有一个简单的图形模型: 1个 User
有N个 SocialUser
。
I'm wondering if there is any way through spring-data-neo4j
to automatically delete all SocialUser
referenced when I remove an User
entity. 我想知道在删除User
实体时是否有任何办法可以通过spring-data-neo4j
自动删除所有引用的SocialUser
。
This is what I've got so far: 到目前为止,这是我得到的:
Domain: 域:
@NodeEntity
public class User implements IdentifiableEntity<String> {
@GraphId
private Long nodeId;
// ...
@RelatedTo(type = "HAS", direction = Direction.OUTGOING)
Set<SocialUser> socialUsers = new HashSet<>();
}
@NodeEntity
public class SocialUser implements BasicNodeEntity {
@GraphId
private Long nodeId;
//...
@RelatedTo(type = "HAS", direction = Direction.INCOMING)
User user;
}
Data : 资料 :
What I've tried: 我试过的
In both cases, only User
is deleted: 在两种情况下,仅删除User
:
At the moment I've encapsulated the deletion of both entities in a @Transactional
method in the User
service. 目前,我已经将两个实体的删除封装在User
服务的@Transactional
方法中。 Something like this: 像这样:
@Autowired
Neo4jOperations template;
@Transactional
public void delete(String userId) throws Exception {
User user = get(userId);
if (user == null) throw new ResourceNotFoundException("user not found");
Set<SocialUser> socialUsers = template.fetch(user.getSocialUsers());
for (SocialUser socialUser : socialUsers) template.delete(socialUser);
userRepository.delete(user);
}
But I'm thinking it's probably not the best way to achieve it. 但是我认为这可能不是实现它的最佳方法。 Also I've thought that it could be better to directly execute a Cypher
statement to delete all referenced nodes.. 我也认为直接执行Cypher
语句删除所有引用的节点可能会更好。
Anyone can advise me how to deal with this? 有人可以建议我如何处理吗? Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!
I know it's been a while, but after being a time working with SDN
and neo4j
, it seems to be that the best way to accomplish this is using a Cypher
query. 我知道已经有一段时间了,但是经过一段时间使用SDN
和neo4j
,似乎最好的方法是使用Cypher
查询。
MATCH (user:User{id:'userId'})-[has:HAS]->(socialUser:SocialUser)
DELETE user, has, socialUser
With SDN, we can take advantadge of repositores: 借助SDN,我们可以利用存储库的优势:
@Repository
public interface UserRepository extends Neo4jRepository<User> {
@Query("MATCH (user:User{id:{id}})-[has:HAS]->(socialUser:SocialUser) DELETE user, has, socialUser")
void delete(String id);
}
Hope it helps other people 希望对别人有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.