等同于SDN4中的template.createRelationBetween [英] equivalent of template.createRelationBetween in SDN4
问题描述
我的项目当前使用的是Spring-Data-Neo4J 3.3.0,我正在尝试使用新的4.0.0.Release版本. 在我的代码中,我有以下代码:
My project is currently use Spring-Data-Neo4J 3.3.0 and I'm trying to use the new 4.0.0.Release version. In my code I have the following code :
neo4jTemplate.createRelationshipBetween(eltOrRel, attribute, valueClass, GraphRelationType.HAS_ATT_VALUE, true)
此代码等效(使用请在新版本的SDK中使用api 中的此方法?
What is the equivalent of this code (which is use this method in api in the new version of SDK please ?
更特别的是,我不知道如何创建给定类型的关系,而是为特定类创建关系.请问如何在密码中写出这样的作品?
More especially I don't know how to create a relation of a given type but for a specific class. How can I write such a creation in cypher please ?
@Luanne这是我的问题的一个小例子.
@Luanne Here is a little example of my problem.
类元素:
@NodeEntity
public class Element {
@GraphId
private Long id;
private int age;
private String uuid;
@Relationship(type = "HAS_ATT_VALUE")
private Set<HasAttValue> values = new HashSet<HasAttValue>();
...
Class Attribute:
Class Attribute :
@NodeEntity
public class Attribute {
@GraphId
private Long id;
private String attName;
和类HasAttValue:
And class HasAttValue :
@RelationshipEntity(type = "HAS_ATT_VALUE")
public class HasAttValue {
@GraphId
private Long id;
@StartNode
Element element;
@EndNode
Attribute attribute;
private String value;
public HasAttValue() {
}
public HasAttValue(Element element, Attribute attribute, String value) {
this.element = element;
this.attribute = attribute;
this.value = value;
this.element.getValues().add(this);
}
在第一种情况下,所有内容都像一个超级按钮,就像您在示例中一样,我具有下图(在服务器浏览器中查看),其值位于HAS_ATT_VALUE RelationshipEntity上:
In this first case, everything works like a charm, and, as in your example I have the following graph (seeing in the server browser) with my value on the HAS_ATT_VALUE relationshipEntity:
(Element)->[HAS_ATT_VALUE]->(attribute)
但是我的问题是在以下情况下(在以前的SDN上运行良好).而不是HasAttValue上一课,我有:
But my problem is in the following case (which was working well with previous SDN). Instead of the HasAttValue previous class, I have :
@RelationshipEntity(type = "HAS_ATT_VALUE")
public abstract class HasAttValue<T> {
@GraphId
private Long id;
@StartNode
Element element;
@EndNode
Attribute attribute;
private T value;
public HasAttValue() {
}
public HasAttValue(Element element, Attribute attribute, T value) {
this.element = element;
this.attribute = attribute;
this.value = value;
this.element.getValues().add(this);
}
具有两个子类. 第一个:
with for example two subclasses. First one :
public class HasBooleanValue extends HasAttValue<Boolean> {
public HasBooleanValue() {
}
public HasBooleanValue(Element elt, Attribute attribut, Boolean value) {
super(elt, attribut, value);
}
}
第二个:
public class HasStringValue extends HasAttValue<String> {
private String locale;
public HasStringValue() {
}
public HasStringValue(Element element, Attribute attribut, String value) {
super(element, attribut, value);
}
在这种情况下,图形如下所示:
In this case the graph is like the following :
(element)->[HAS_ATT_VALUE]->(HasBooleanValue|HasStringValue)->[ATTRIBUTE]->(Attribute)
和图形中的另一个弧 (element)<-[ELEMENT]-(HasBooleanValue | HasStringValue)
and another arc in the graphe (element)<-[ELEMENT]-(HasBooleanValue|HasStringValue)
那么我该怎么做才能始终具有(element)-> [HAS_ATT_VALUE]->(attribute),其中"has_att_value"是一个包含数据但在我的Java代码中具有不同实现的关系性?再次,当我使用neo4jTemplate.createRelationshipBetween(eltOrRel,attribute,valueClass,GraphRelationType.HAS_ATT_VALUE,true)创建我的"hasAttValue" RelationshipEntity时,这在SDN3中运行良好.
So how can I do to always have (element)->[HAS_ATT_VALUE]->(attribute) where "has_att_value" is a relationshipentity containing datas but having diffent impletations in my java code ? Again, this was working well in SDN3 when I used the neo4jTemplate.createRelationshipBetween(eltOrRel, attribute, valueClass, GraphRelationType.HAS_ATT_VALUE, true) to create my "hasAttValue" relationshipEntity.
非常感谢您
推荐答案
在SDN 4.0中,不需要使用Neo4jTemplate方法显式创建关系.保留定义了@Relationships的实体就足以 @ RelationshipEntity .
In SDN 4.0, relationships do not need to be created explicitly using Neo4jTemplate methods. Persisting the entity on which @Relationships are defined is enough to create relationships. If you have properties on the relationship, then you'd need a @RelationshipEntity.
An explanation of object models in SDN 4.0 can be found here http://graphaware.com/neo4j/2015/09/03/sdn-4-object-model.html
根据来自@clement的其他信息进行更新:
只需将注释从HasAttValue
类移动到每个子类,例如
Just move the @RelationshipEntity
annotation from the HasAttValue
class to each subclass, for example
@RelationshipEntity(type = "HAS_ATT_VALUE")
public class HasBooleanValue extends HasAttValue<Boolean> {
请注意,由于有关抽象关系实体的问题已得到解决,因此您将需要最新的OGM快照.请使用
Note that you will need the latest OGM snapshot since an issue around abstract relationship entities was just fixed. Please use
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm</artifactId>
<version>1.1.3-SNAPSHOT</version>
</dependency>
<repository>
<id>neo4j-snapshots</id>
<url>http://m2.neo4j.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
或使用SDN 4.1快照
Or use the SDN 4.1 snapshot
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>4.1.0.BUILD-SNAPSHOT</version>
</dependency>
然后您的图形应该看起来像
Then your graph should look like
直接使用Cypher并不是一个好主意,因为您必须能够查找节点(可能是通过ID),这意味着必须首先保存它们.
Using Cypher directly is not a good idea as you'd have to be able to look up the nodes (maybe by ID), which would mean they have to be saved first.
这篇关于等同于SDN4中的template.createRelationBetween的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!