Im using Neo4J OGM + Play Framework since 2 weeks succesfully, but today it doesn't work anymore. Every time I change something in my code, no matter if in a NodeEntity Class or in any other Class, it causes a java.lang.ClassNotFoundException when I try to get something from the database using the find method from org.neo4j.ogm.session.Session. Only if I clear the database and refill it I'm able to insert and get my NodesEntities.
Java version: 1.8
Scala version: 2.11.7
Sbt version: 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)
}
Exception
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)
Can anyone tell me if I'm going to go crazy or if it's still to be saved?
I have tried your code and found that this error only occurs with a GET-request. I found this when I tried an insert with a POST-request. In this case, the "find"-method has found the appropriate class, but the subsequent GET-request does not. But I don't understand why it doesn't work with a GET-request. Maybe the header is different than a POST-request? It is also not a permanent solution to give up the get, to get the resource.