我使用Neo4J OGM + Play Framework已经成功使用了2周,但今天它已不再适用了。每当我在代码中更改某些内容时,无论是在NodeEntity类还是在任何其他类中,当我尝试使用org.neo4j.ogm中的find方法从数据库中获取内容时,都会导致java.lang.ClassNotFoundException。 session.Session。只有当我清除数据库并重新填充它时,我才能插入并获取我的NodesEntities。
Java版本:1.8
Scala版本:2.11.7
Sbt版本:2.6.3
build.sbt :
libraryDependencies += "org.neo4j" % "neo4j-ogm-core" % "3.0.0-RC1"
libraryDependencies += "org.neo4j" % "neo4j-ogm-bolt-driver" % "3.0.0-RC1"
Neo4JSessionFactory.java
public class Neo4jSessionFactory {
private Config config;
@Inject
private Neo4jSessionFactory(Config config) {
this.config = config;
}
public Session getNeo4jSession() {
String uri = config.getString("ogm.db.uri");
String username = config.getString("ogm.db.username");
String password = config.getString("ogm.db.password");
List<String> modelList = config.getStringList("ogm.db.models");
String[] models = modelList.toArray(new String[modelList.size()]);
Configuration configuration = new Configuration.Builder()
.uri(uri)
.credentials(username, password)
.build();
return new SessionFactory(configuration, models).openSession();
}
}
application.conf
ogm{
db{
uri = "bolt://XXX.de:7687"
username = "XXX"
password = "XXX"
models = ["neo4j.nodes", "neo4j.relationships", "neo4j.entities"]
}
}
UserNode.java
@NodeEntity(label = "UserNode")
public class UserNode extends AbstractNode {
@JsonProperty("username")
private String username;
@JsonProperty("firstname")
private String firstname;
@JsonProperty("lastname")
private String lastname;
@JsonProperty("email")
private String email;
@JsonProperty("password")
private String password;
@JsonProperty("picture")
private String picture;
@Relationship(type = Friendship.TYPE)
@JsonProperty("friendships")
private Set<Friendship> friendships = new HashSet<>();
@Relationship(type = Posted.TYPE)
@JsonProperty("postings")
private Set<Posted> postings = new HashSet<>();
@Relationship(type = Pinned.TYPE, direction = Relationship.INCOMING)
@JsonProperty("pinnings")
private Set<Pinned> pinnings = new HashSet<>();
public UserNode() {
}
}
UserService.java
public class UserService extends AbstractService<UserNode> {
@Inject
public UserService(Neo4jSessionFactory neo4jSessionFactory) {
super(neo4jSessionFactory);
}
@Override
public Class<UserNode> getEntityType() {
return UserNode.class;
}
}
AbstractService.java
public abstract class AbstractService<T extends AbstractNode> {
private static final int DEPTH_LIST = 1;
private static final int DEPTH_ENTITY = 1;
protected Session session;
@Inject
public AbstractService(Neo4jSessionFactory neo4jSessionFactory) {
this.session = neo4jSessionFactory.getNeo4jSession();
}
public Collection<T> findAll() {
return session.loadAll(getEntityType(), DEPTH_LIST); <-- (AbstractService:27)
}
public Optional<T> find(Long id) {
return Optional.ofNullable(session.load(getEntityType(), id, DEPTH_ENTITY));
}
public void delete(Long id) {
session.delete(session.load(getEntityType(), id));
}
public Optional<T> createOrUpdate(T entity){
T updated = find(entity.getId())
.map(existing -> {
entity.setCreated(existing.getCreated());
return entity;
}).orElse(entity);
session.save(updated, DEPTH_ENTITY);
return find(updated.getId());
}
}
AbstactController.java
public Result all(){
return toJsonResult(service.findAll()); <-- (AbstractCRUDController.java:19)
}
例外
play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[MappingException: Error mapping GraphModel to instance of neo4j.nodes.UserNode]]
at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:255)
at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:180)
at play.core.server.AkkaHttpServer$$anonfun$13$$anonfun$apply$1.applyOrElse(AkkaHttpServer.scala:251)
at play.core.server.AkkaHttpServer$$anonfun$13$$anonfun$apply$1.applyOrElse(AkkaHttpServer.scala:250)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:344)
at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:343)
at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
at play.api.libs.streams.Execution$trampoline$.execute(Execution.scala:70)
at scala.concurrent.impl.CallbackRunnable.executeWithValue(Promise.scala:40)
at scala.concurrent.impl.Promise$DefaultPromise.scala$concurrent$impl$Promise$DefaultPromise$$dispatchOrAddCallback(Promise.scala:280)
Caused by: org.neo4j.ogm.exception.MappingException: Error mapping GraphModel to instance of neo4j.nodes.UserNode
at org.neo4j.ogm.context.GraphEntityMapper.mapEntities(GraphEntityMapper.java:168)
at org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:124)
at org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:89)
at org.neo4j.ogm.session.delegates.LoadByTypeDelegate.loadAll(LoadByTypeDelegate.java:65)
at org.neo4j.ogm.session.delegates.LoadByTypeDelegate.loadAll(LoadByTypeDelegate.java:99)
at org.neo4j.ogm.session.Neo4jSession.loadAll(Neo4jSession.java:167)
at neo4j.services.AbstractService.findAll(AbstractService.java:27)
at controllers.AbstractCRUDController.all(AbstractCRUDController.java:19)
at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$2$$anonfun$apply$2.apply(Routes.scala:364)
at router.Routes$$anonfun$routes$1$$anonfun$applyOrElse$2$$anonfun$apply$2.apply(Routes.scala:364)
Caused by: org.neo4j.ogm.exception.MappingException: Unable to load class with FQN: neo4j.nodes.UserNode
at org.neo4j.ogm.metadata.reflect.EntityFactory.instantiateObjectFromTaxa(EntityFactory.java:109)
at org.neo4j.ogm.metadata.reflect.EntityFactory.newObject(EntityFactory.java:58)
at org.neo4j.ogm.context.GraphEntityMapper.mapNodes(GraphEntityMapper.java:179)
at org.neo4j.ogm.context.GraphEntityMapper.mapEntities(GraphEntityMapper.java:165)
at org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:124)
at org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:89)
at org.neo4j.ogm.session.delegates.LoadByTypeDelegate.loadAll(LoadByTypeDelegate.java:65)
at org.neo4j.ogm.session.delegates.LoadByTypeDelegate.loadAll(LoadByTypeDelegate.java:99)
at org.neo4j.ogm.session.Neo4jSession.loadAll(Neo4jSession.java:167)
at neo4j.services.AbstractService.findAll(AbstractService.java:27)
Caused by: java.lang.ClassNotFoundException: neo4j.nodes.UserNode
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.neo4j.ogm.metadata.reflect.EntityFactory.instantiateObjectFromTaxa(EntityFactory.java:106)
at org.neo4j.ogm.metadata.reflect.EntityFactory.newObject(EntityFactory.java:58)
at org.neo4j.ogm.context.GraphEntityMapper.mapNodes(GraphEntityMapper.java:179)
at org.neo4j.ogm.context.GraphEntityMapper.mapEntities(GraphEntityMapper.java:165)
at org.neo4j.ogm.context.GraphEntityMapper.map(GraphEntityMapper.java:124)
任何人都可以告诉我,我是否会发疯或者是否还要保存?
答案 0 :(得分:2)
我已经尝试过您的代码,发现只有GET请求才会出现此错误。 当我尝试使用POST请求插入时,我发现了这一点。在这种情况下,&#34; find&#34; -method找到了合适的类,但后续的GET请求却没有。但我不明白为什么它不能使用GET请求。也许标题与POST请求不同?它也不是放弃获取资源的永久解决方案。