将实体与Neo4j中适当的关系相关联? [英] Relating an entity to a relationship proper in Neo4j?

查看:286
本文介绍了将实体与Neo4j中适当的关系相关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Neo4j对项目,人员和项目角色之间的关系进行建模.每个项目都有一个称为项目经理"的角色和一个称为导演"的角色.我要在数据模型中完成的工作是能够说对于项目A,主管是X员工".就我的目的而言,项目",职员"和角色"都是实体(与属性相对)非常重要. Neo4j有可能吗?简单来说,Neo4j中的关联实体可能吗?在MySQL中,这将用一个具有唯一id列和三个外键列的联结表来表示,三个外键列分别用于项目,人员和角色,这将使我能够将这些实体之间的关系标识为一个实体本身.有想法吗?

I'm attempting to use Neo4j to model the relationship between projects, staff, and project roles. Each project has a role called "project manager" and a role called "director". What I'm trying to accomplish in the data model is the ability to say "for project A, the director is staff X." For my purposes, it's important that "project", "staff", and "role" are all entities (as opposed to properties). Is this possible in Neo4j? In simpler terms, are associative entities possible in Neo4j? In MySQL, this would be represented with a junction table with a unique id column and three foreign key columns, one for project, staff, and role respectively, which would allow me to identify the relationship between those entities as an entity itself. Thoughts?

推荐答案

@wassgren的回答很可靠,值得考虑.

@wassgren's answer is a solid one, worth considering.

我将提供另一种选择.也就是说,您可以"确认"该关系​​.当您建立关系并将其转变为节点时,意义就是某种意义.您正在采用抽象关联(人员与项目之间的关系),并将其转换为具体实体(角色).所有其他答案选项基本上都涉及两个节点ProjectStaff,并且关系有所不同它们之间.这些方法并不确定角色,而是将其存储为关系的属性或标签.

I'll offer one additional option. That is, you can "Reify" that relationship. Reificiation is sort of when you take a relationship and turn it into a node. You're taking an abstract association (relationship between staff and project) and your'e turning it into a concrete entity (a Role) All of the other answer options involve basically two nodes Project and Staff, with variations on relationships between them. These approaches do not reify role, but store it as a property, or a label, of a relationship.

(director:Staff {name: "Joe"})-[:plays]->(r:Role {label:"Director"})-[:member_of]->(p:Project { name: "Project X"});

所以...人们不直接为项目做贡献,角色却不.人们在扮演角色.这很直观.

So...people don't contribute to projects directly, roles do. And people play roles. Which makes an intuitive sense.

此方法的优点在于,您可以将角色"视为头等公民,并维护与之相关的关系和属性.如果不将角色"拆分为单独的节点,则将无法将关系挂起.此外,如果您在伪装成角色的关系中添加额外的属性,则可能最终会困惑于何时将属性应用于角色,以及何时将其应用于职员与项目之间的关联.

The advantages of this approach is that you get to treat the "Role" as a first-class citizen, and assert relationships and properties about it. If you don't split the "Role" out into a separate node, you won't be able to hang relationships off of the node. Further, if you add extra properties to a relationship that is masquerading as a role, you might end up with confusions about when a property applies to the role, and when it applies to the association between a staff member and a project.

想知道谁在一个项目上?就是这样:

Want to know who is on a project? That's just:

  MATCH (p:Project {label: "Project X"})<-[:member_of]-(r:Role)<-[:plays]-(s:Staff)
   RETURN s;

因此,我认为从长远来看,我的建议更加灵活,但对您来说可能也太过致命了.

So I think what I'm suggesting is more flexible for the long term, but it might also be overkill for you.

考虑一个假设的未来需求:我们希望将角色与技术级别或职位类别相关联. IE.项目经理应始终为副总裁或更高职位(愚蠢的例子).如果您的角色是恋爱关系,那么您将无法做到.如果您的角色是适当的节点,则可以.

Consider a hypothetical future requirement: we want to associate roles with a technical level or job category. I.e. the project manager should always be a VP or higher (silly example). If your role is a relationship, you can't do that. If your role is a proper node, you can.

这篇关于将实体与Neo4j中适当的关系相关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆