java.lang.ClassNotFoundException 每次我更改我的代码时(Neo4j OGM + 播放!)

java.lang.ClassNotFoundException Every time I change something in my code (Neo4j OGM + Play!)

我成功使用 Neo4J OGM + Play Framework 两周了,但今天它不再工作了。每次我在代码中更改某些内容时,无论是在 NodeEntity Class 还是在任何其他 Class 中,当我尝试使用 find 从数据库中获取内容时都会导致 java.lang.ClassNotFoundException来自 org.neo4j.ogm.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: 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$$anonfun$apply.applyOrElse(AkkaHttpServer.scala:251)
at play.core.server.AkkaHttpServer$$anonfun$$anonfun$apply.applyOrElse(AkkaHttpServer.scala:250)
at scala.concurrent.Future$$anonfun$recoverWith.apply(Future.scala:344)
at scala.concurrent.Future$$anonfun$recoverWith.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$$anonfun$applyOrElse$$anonfun$apply.apply(Routes.scala:364)
at router.Routes$$anonfun$routes$$anonfun$applyOrElse$$anonfun$apply.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)

谁能告诉我我是要疯了还是还有救?

我试过你的代码,发现这个错误只发生在GET-request。 我在尝试使用 POST-request 插入时发现了这一点。在这种情况下,"find" 方法找到了合适的 class,但随后的 GET-request 却没有。但我不明白为什么它不适用于 GET-request。也许 header 与 POST-request 不同?放弃get也不是长久之计,去获取资源