Neo4j OGM 映射异常
Neo4j OGM MappingException
我正在尝试获取 OGM 上密码查询的结果。
基本上,我一直在关注这个教程:OGM Tutorial.
我有一个实体接口(与教程中的接口相同):
public abstract class Entity {
private Long id;
public Long getId() {
return id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || id == null || getClass() != o.getClass()) return false;
Entity entity = (Entity) o;
if (!id.equals(entity.id)) return false;
return true;
}
@Override
public int hashCode() {
return (id == null) ? -1 : id.hashCode();
}
}
我有一个名为 MyClass 的 class 继承自它:
@NodeEntity
public class MyClass extends Entity {
private String field;
@Relationship(type="POINTS", direction=Relationship.OUTGOING)
private Set<Motif> next = new HashSet<Motif>();
public MyClass(String field) {
this.field = field;
}
public String getField(){
return this.field;
}
public void setField(String field) {
this.field = field;
}
public Set<Motif> getNext() {
return next;
}
public void setNext(Set<Motif> next) {
this.next = next;
}
}
我正在毫无问题地填充数据库,我也可以在浏览器中执行密码查询。
但是,当我尝试使用此搜索对我的应用程序执行查询时 class:
public class Searcher {
public Searcher() {
}
public void search() {
String query = "MATCH (a:MyClass)-[r:POINTS]->(b:MyClass) WHERE a.field='B315' RETURN b";
Iterable<MyClass> objs = Neo4jSessionFactory.getInstance().getNeo4jSession().query(MyClass.class, query, Collections.EMPTY_MAP);
for(MyClass obj: objs) {
System.out.println(obj.getField());
}
}
}
我收到以下错误:
Exception in thread "main" org.neo4j.ogm.exception.MappingException: Error mapping GraphModel to instance of <package>.MyClass
at org.neo4j.ogm.context.GraphEntityMapper.mapEntities(GraphEntityMapper.java:145)
at org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:117)
at org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:81)
at org.neo4j.ogm.session.delegates.ExecuteQueriesDelegate.executeAndMap(ExecuteQueriesDelegate.java:111)
at org.neo4j.ogm.session.delegates.ExecuteQueriesDelegate.query(ExecuteQueriesDelegate.java:82)
at org.neo4j.ogm.session.Neo4jSession.query(Neo4jSession.java:323)
Caused by: org.neo4j.ogm.exception.MappingException: Unable to instantiate class <package>.MyClass
at org.neo4j.ogm.annotations.EntityFactory.instantiate(EntityFactory.java:137)
at org.neo4j.ogm.annotations.EntityFactory.instantiateObjectFromTaxa(EntityFactory.java:110)
at org.neo4j.ogm.annotations.EntityFactory.newObject(EntityFactory.java:61)
at org.neo4j.ogm.context.GraphEntityMapper.mapNodes(GraphEntityMapper.java:156)
at org.neo4j.ogm.context.GraphEntityMapper.mapEntities(GraphEntityMapper.java:142)
... 7 more
Caused by: java.lang.NoSuchMethodException: <package>.MyClass.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.getDeclaredConstructor(Class.java:2178)
at org.neo4j.ogm.annotations.EntityFactory.instantiate(EntityFactory.java:133)
... 11 more
所以我可能遗漏了一些东西,因为显然我的 MyClass class 无法映射,即使它是一个节点实体。
MyClass.java
requires a no arg constructor。这就是它不能被 OGM 实例化的原因。
我正在尝试获取 OGM 上密码查询的结果。 基本上,我一直在关注这个教程:OGM Tutorial.
我有一个实体接口(与教程中的接口相同):
public abstract class Entity {
private Long id;
public Long getId() {
return id;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || id == null || getClass() != o.getClass()) return false;
Entity entity = (Entity) o;
if (!id.equals(entity.id)) return false;
return true;
}
@Override
public int hashCode() {
return (id == null) ? -1 : id.hashCode();
}
}
我有一个名为 MyClass 的 class 继承自它:
@NodeEntity
public class MyClass extends Entity {
private String field;
@Relationship(type="POINTS", direction=Relationship.OUTGOING)
private Set<Motif> next = new HashSet<Motif>();
public MyClass(String field) {
this.field = field;
}
public String getField(){
return this.field;
}
public void setField(String field) {
this.field = field;
}
public Set<Motif> getNext() {
return next;
}
public void setNext(Set<Motif> next) {
this.next = next;
}
}
我正在毫无问题地填充数据库,我也可以在浏览器中执行密码查询。
但是,当我尝试使用此搜索对我的应用程序执行查询时 class:
public class Searcher {
public Searcher() {
}
public void search() {
String query = "MATCH (a:MyClass)-[r:POINTS]->(b:MyClass) WHERE a.field='B315' RETURN b";
Iterable<MyClass> objs = Neo4jSessionFactory.getInstance().getNeo4jSession().query(MyClass.class, query, Collections.EMPTY_MAP);
for(MyClass obj: objs) {
System.out.println(obj.getField());
}
}
}
我收到以下错误:
Exception in thread "main" org.neo4j.ogm.exception.MappingException: Error mapping GraphModel to instance of <package>.MyClass
at org.neo4j.ogm.context.GraphEntityMapper.mapEntities(GraphEntityMapper.java:145)
at org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:117)
at org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:81)
at org.neo4j.ogm.session.delegates.ExecuteQueriesDelegate.executeAndMap(ExecuteQueriesDelegate.java:111)
at org.neo4j.ogm.session.delegates.ExecuteQueriesDelegate.query(ExecuteQueriesDelegate.java:82)
at org.neo4j.ogm.session.Neo4jSession.query(Neo4jSession.java:323)
Caused by: org.neo4j.ogm.exception.MappingException: Unable to instantiate class <package>.MyClass
at org.neo4j.ogm.annotations.EntityFactory.instantiate(EntityFactory.java:137)
at org.neo4j.ogm.annotations.EntityFactory.instantiateObjectFromTaxa(EntityFactory.java:110)
at org.neo4j.ogm.annotations.EntityFactory.newObject(EntityFactory.java:61)
at org.neo4j.ogm.context.GraphEntityMapper.mapNodes(GraphEntityMapper.java:156)
at org.neo4j.ogm.context.GraphEntityMapper.mapEntities(GraphEntityMapper.java:142)
... 7 more
Caused by: java.lang.NoSuchMethodException: <package>.MyClass.<init>()
at java.lang.Class.getConstructor0(Class.java:3082)
at java.lang.Class.getDeclaredConstructor(Class.java:2178)
at org.neo4j.ogm.annotations.EntityFactory.instantiate(EntityFactory.java:133)
... 11 more
所以我可能遗漏了一些东西,因为显然我的 MyClass class 无法映射,即使它是一个节点实体。
MyClass.java
requires a no arg constructor。这就是它不能被 OGM 实例化的原因。