[英]How to add labels dynamically to nodes in Neo4j from neo4j-ogm or spring-data-neo4j?
When I create a node, I want to add multiple labels, known at run-time, to the node. 创建节点时,我想向该节点添加运行时已知的多个标签。 Is it possible to this in neo4j-ogm or spring-data-neo4j?
在neo4j-ogm或spring-data-neo4j中可能吗?
当前版本不支持此功能,但已在路线图中。
Add some dependences 添加一些依赖
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-core</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
Create an Enitity with lombok accessors 使用龙目岛访问器创建Enitity
@NodeEntity
@Data
public class Content{
@Id
@GeneratedValue
private Long id; //Internal Neo4j Identifier. DONT TOUCH
// Your Bns Logic identifier
private Long myId
@Properties
private Map<String, String> properties = new HashMap<>();
@Labels
private List<String> labels = new ArrayList<>();
}
A Repository for your Entity 您实体的存储库
public interface ContentRepository extends Neo4jRepository<Content, Long> {
}
A simple controller to add your labels and properties in the Node 一个简单的控制器,用于在节点中添加标签和属性
@RestController
@RequestMapping( produces = MediaType.APPLICATION_JSON_VALUE)
public class ContentController {
@Autowired
ContentRepository contentRepository;
@ApiOperation(value = "Create a Node", notes = "create a node", response = String.class)
@ApiResponses({
@ApiResponse(code = 201, message = "Success", response = String.class)
})
@PostMapping("/api/content")
public ResponseEntity<MyDTO> createNode(@RequestBody MyDTO requestWrapper ) {
//Create Database Entity from DTO
Content content = new Content();
//Add Labels
content.getLabels().addAll(requestWrapper.getLabelList());
//Add properties
requestWrapper.getHmap().forEach((k,v)->content.getProperties().put(k,v));
try {
contentRepository.save(content);
requestWrapper.setId(content.getId());
} catch (Exception e){
//e.printStackTrace();
}
return new ResponseEntity< MyDTO >(requestWrapper, HttpStatus.CREATED);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.