http://neo4j.com/docs/ogm/java/stable/の Neo4j OGM ガイドに従っています。
Relationship エンティティの場合、開始ノードと終了ノードが必要です。例を少し変更しました (このように簡単にするため) -
@NodeEntity
public class Student extends Entity {
private String name;
@Relationship(type= "ENROLLED")
private Enrollment enrollment = new Enrollment();
public String getName() {
return name;
}
public Enrollment getEnrollment() {
return enrollment;
}
public void setEnrollment(Enrollment enrollment) {
this.enrollment = enrollment;
}
public Student() {
}
public Student(String name) {
this.name = name;
}
}
@NodeEntity
public class Course extends Entity {
private String name;
public String getName() {
return name;
}
@Relationship(type= "ENROLLED", direction= Relationship.INCOMING)
private Enrollment enrollment = new Enrollment();
public Enrollment getEnrollment() {
return enrollment;
}
public void setEnrollment(Enrollment enrollment) {
this.enrollment = enrollment;
}
public Course() {
}
public Course(String name) {
this.name = name;
}
}
@RelationshipEntity(type = "ENROLLED")
public class Enrollment extends Entity {
@StartNode
private Student student;
@EndNode
private Course course;
private Date enrolledDate;
public Student getStudent() {
return student;
}
public Course getCourse() {
return course;
}
public Date getEnrolledDate() {
return enrolledDate;
}
public Enrollment() {
}
public Enrollment(Student student, Course course, Date enrolledDate) {
this.student = student;
this.course = course;
this.enrolledDate = enrolledDate;
}
}
これを Neo4j に保存しようとすると、正常に動作します。ただし、私のシナリオでは、 StartNode オブジェクトと EndNode オブジェクトのタイプは同じです -
@NodeEntity
public class MyObject extends Entity {
private String name;
@Relationship(type="CONNECTION")
private MyConnection startConnection = new MyConnection();
@Relationship(type="CONNECTION", direction= Relationship.INCOMING)
private MyConnection endConnection = new MyConnection();
public String getName() {
return name;
}
public MyConnection getStartConnection() {
return startConnection;
}
public void setStartConnection(MyConnection myConnection) {
this.startConnection = myConnection;
}
public MyConnection getEndConnection() {
return endConnection;
}
public void setEndConnection(MyConnection endConnection) {
this.endConnection = endConnection;
}
public MyObject() {
super();
}
public MyObject(String name) {
super();
this.name = name;
}
}
@RelationshipEntity(type="CONNECTION")
public class MyConnection extends Entity {
@StartNode
private MyObject start;
@EndNode
private MyObject end;
private String name;
public String getName() {
return name;
}
public MyConnection() {
super();
}
public MyConnection(MyObject start, MyObject end, String name) {
super();
this.start = start;
this.end = end;
this.name = name;
}
}
これらを使用して保存しようとすると-
public class Main {
public static void main(String[] args) {
Session session = Neo4jSessionFactory.getInstance().getNeo4jSession();
Student s = new Student("manoj");
Course c = new Course("physics");
Enrollment e = new Enrollment(s, c, new Date());
s.setEnrollment(e);
c.setEnrollment(e);
MyObject startObj = new MyObject("Start Object");
MyObject endObj = new MyObject("End Object");
MyConnection conn = new MyConnection(startObj, endObj, "Connection");
startObj.setStartConnection(conn);
endObj.setEndConnection(conn);
try(Transaction tx = session.beginTransaction()) {
session.save(s);
session.save(c);
session.save(e);
session.save(startObj);
session.save(endObj);
session.save(conn);
tx.commit();
}
}
}
学生、コース、登録オブジェクトは保存されますが、2 つの MyObject オブジェクトと MyConnection オブジェクトは保存されず、次の例外が発生します -
Exception in thread "main" java.lang.NullPointerException
at org.neo4j.ogm.metadata.MetaData.classInfo(MetaData.java:76)
at org.neo4j.ogm.mapper.EntityGraphMapper.mapRelationshipEntity(EntityGraphMapper.java:389)
at org.neo4j.ogm.mapper.EntityGraphMapper.link(EntityGraphMapper.java:319)
at org.neo4j.ogm.mapper.EntityGraphMapper.mapEntityReferences(EntityGraphMapper.java:262)
at org.neo4j.ogm.mapper.EntityGraphMapper.mapEntity(EntityGraphMapper.java:154)
at org.neo4j.ogm.mapper.EntityGraphMapper.mapRelatedEntity(EntityGraphMapper.java:528)
at org.neo4j.ogm.mapper.EntityGraphMapper.mapRelationshipEntity(EntityGraphMapper.java:420)
at org.neo4j.ogm.mapper.EntityGraphMapper.link(EntityGraphMapper.java:319)
at org.neo4j.ogm.mapper.EntityGraphMapper.mapEntityReferences(EntityGraphMapper.java:262)
at org.neo4j.ogm.mapper.EntityGraphMapper.mapEntity(EntityGraphMapper.java:154)
at org.neo4j.ogm.mapper.EntityGraphMapper.map(EntityGraphMapper.java:87)
at org.neo4j.ogm.session.delegates.SaveDelegate.save(SaveDelegate.java:65)
at org.neo4j.ogm.session.delegates.SaveDelegate.save(SaveDelegate.java:41)
at org.neo4j.ogm.session.Neo4jSession.save(Neo4jSession.java:370)
at neo4j.ogm.ex.Main.main(Main.java:37)
これを解決するのを手伝ってくれませんか - 1) StartNode オブジェクトと EndNode オブジェクトが異なるタイプである必要がありますか? 2) 私のコードに何か問題がありますか、それとも Neo4j OGM の欠点ですか?
前もって感謝します、
マノジ。
Luanneの提案を試した後の更新 -
ありがとうルアン。URLを別の方法で指定する必要がありましたが、あなたの提案を試しました。私が使用した - http://m2.neo4j.org/content/repositories/snapshots は、デフォルトで https を使用し、セキュリティ例外が発生し、この依存関係がダウンロードされなかったためです。
とにかく、1.1.1-SNAPSHOT バージョンでは、まだ以下のエラーが発生します -
Exception in thread "main" java.lang.NullPointerException
at org.neo4j.ogm.metadata.MetaData.classInfo(MetaData.java:80)
at org.neo4j.ogm.mapper.EntityGraphMapper.haveRelationEndsChanged(EntityGraphMapper.java:391)
at org.neo4j.ogm.mapper.EntityGraphMapper.getRelationshipBuilder(EntityGraphMapper.java:362)
at org.neo4j.ogm.mapper.EntityGraphMapper.link(EntityGraphMapper.java:325)
at org.neo4j.ogm.mapper.EntityGraphMapper.mapEntityReferences(EntityGraphMapper.java:276)
at org.neo4j.ogm.mapper.EntityGraphMapper.mapEntity(EntityGraphMapper.java:157)
at org.neo4j.ogm.mapper.EntityGraphMapper.mapRelatedEntity(EntityGraphMapper.java:571)
at org.neo4j.ogm.mapper.EntityGraphMapper.mapRelationshipEntity(EntityGraphMapper.java:473)
at org.neo4j.ogm.mapper.EntityGraphMapper.link(EntityGraphMapper.java:329)
at org.neo4j.ogm.mapper.EntityGraphMapper.mapEntityReferences(EntityGraphMapper.java:276)
at org.neo4j.ogm.mapper.EntityGraphMapper.mapEntity(EntityGraphMapper.java:157)
at org.neo4j.ogm.mapper.EntityGraphMapper.map(EntityGraphMapper.java:90)
at org.neo4j.ogm.session.delegates.SaveDelegate.save(SaveDelegate.java:67)
at org.neo4j.ogm.session.delegates.SaveDelegate.save(SaveDelegate.java:43)
at org.neo4j.ogm.session.Neo4jSession.save(Neo4jSession.java:376)
at neo4j.ogm.ex.Main.main(Main.java:37)