代码之家  ›  专栏  ›  技术社区  ›  Vishrant

Neo4J中节点之间关系的自定义数据类型或HashMap

  •  0
  • Vishrant  · 技术社区  · 7 年前

    是否可以将节点之间的关系值作为用户定义的类,或者它应该是原始数据类型。例如:

    @RelationshipEntity(type = "ALIGNED_WITH")
    public class AlignedWith {
    
        @GraphId
        private Long id;
    
        private Set<DeptEndorsement> deptEndorsement = new HashSet<>();
    
        @StartNode
        private Term startTerm;
    
        @EndNode
        private Term endTerm;
    
        // getters and setters
    
    }
    
    
    public class DeptEndorsement {
    
        private String deptName;
        private Integer endorsementCount;
    
        // getters and setters
    
    }
    
    
    @NodeEntity
    public class Term {
    
        @GraphId
        private Long id;
    
        private String termName;
    
        @Relationship(type = "ALIGNED_WITH", direction = Relationship.OUTGOING)
        private List<AlignedWith> alignedWith = new ArrayList<>();
    
        public void addAlignedWith(AlignedWith alignedWith) {
            this.alignedWith.add(alignedWith);
        }
    
        // getters and setters
    
    }
    

    如果你观察 DeptEndorsement 是一个自定义类,我想将其作为关系的值 AlignedWith

    或者,有没有可能 HashMap (示例: HashMap<String, Integer> )作为节点之间关系的值?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Vishrant    7 年前

    以下是我对这个问题的发现,Neo4J中支持的数据类型有:

    布尔型、长型、双精度、字符串、列表。可以进行类型转换的类型有Short、Integer、Float(虽然需要指定允许转换,但请检查下面的if条件) Ref. MapCompositeConverter.java

    以下条件负责检查类型是否受Neo4J支持,在我的情况下,我没有提供 allowCast 成为 true 因此 java.lang.Integer 不可浇铸:

    if (isCypherType(entryValue) || (allowCast && canCastType(entryValue)))
    
    private boolean isCypherType(Object entryValue) {
        return cypherTypes.contains(entryValue.getClass()) || List.class.isAssignableFrom(entryValue.getClass());
    }
    

    我从 Java语言lang.整数 java.lang.Long 一切正常。您不能像我的情况那样使用自定义数据类型 DeptEndorsement

    下面是我的属性在关系中的外观。

    @Properties
    private Map<String, Long> deptEndorsement = new HashMap<>();
    

    @Properties 注释在中可用 neo4j-ogm-core 3.0.0+ Ref: Compatibility