如何从Neo4j-ogm或spring-data-neo4j动态地向Neo4j中的节点添加标签?

时间:2015-11-15 19:44:38

标签: neo4j spring-data-neo4j-4 neo4j-ogm

当我创建节点时,我想在节点上添加多个运行时已知的标签。在neo4j-ogm或spring-data-neo4j中可以这样吗?

2 个答案:

答案 0 :(得分:2)

当前版本不支持此功能,但在路线图上。

答案 1 :(得分:0)

添加一些依赖

    <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>

使用lombok访问者创建一个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<>();
}

您实体的存储库

public interface ContentRepository extends Neo4jRepository<Content, Long> {
}

一个简单的控制器,用于在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);
    }