【问题标题】:How to add labels dynamically to nodes in Neo4j from neo4j-ogm or spring-data-neo4j?如何从 neo4j-ogm 或 spring-data-neo4j 向 Neo4j 中的节点动态添加标签?
【发布时间】:2015-11-15 19:44:37
【问题描述】:

当我创建一个节点时,我想向节点添加多个在运行时已知的标签。在 neo4j-ogm 或 spring-data-neo4j 中是否有可能?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    【解决方案2】:

    添加一些依赖

        <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 访问器创建一个实体

    @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> {
    }
    

    在节点中添加标签和属性的简单控制器

    @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);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多