I've got a simple graph model: 1 User
has N SocialUser
.
我有一个简单的图形模型:1用户有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.
我想知道是否有任何方法通过spring-data-neo4j自动删除删除User实体时引用的所有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:
我尝试过的:
- Delete a user through GraphRepository
- Delete a user through Neo4jTemplate
通过GraphRepository删除用户
通过Neo4jTemplate删除用户
In both cases, only User
is deleted:
在这两种情况下,只删除用户:
At the moment I've encapsulated the deletion of both entities in a @Transactional
method in the User
service. Something like this:
目前,我已在用户服务的@Transactional方法中封装了两个实体的删除。像这样的东西:
@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!
任何人都可以建议我如何处理这个?任何帮助将不胜感激。谢谢!
1 个解决方案
#1
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,我们可以利用repositores:
@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
希望它能帮助其他人
#1
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,我们可以利用repositores:
@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
希望它能帮助其他人