[英]spring-data-neo4j remove nodeEntity and all referenced nodes
我有一个简单的图形模型: 1个 User
有N个 SocialUser
。
我想知道在删除User
实体时是否有任何办法可以通过spring-data-neo4j
自动删除所有引用的SocialUser
。
到目前为止,这是我得到的:
域:
@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;
}
资料 :
我试过的
在两种情况下,仅删除User
:
目前,我已经将两个实体的删除封装在User
服务的@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);
}
但是我认为这可能不是实现它的最佳方法。 我也认为直接执行Cypher
语句删除所有引用的节点可能会更好。
有人可以建议我如何处理吗? 任何帮助将不胜感激。 谢谢!
我知道已经有一段时间了,但是经过一段时间使用SDN
和neo4j
,似乎最好的方法是使用Cypher
查询。
MATCH (user:User{id:'userId'})-[has:HAS]->(socialUser:SocialUser)
DELETE user, has, socialUser
借助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);
}
希望对别人有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.