Spring 数据 Neo4j 4 未存储任何数据

Spring Data Neo4j 4 is not storing any data

我在我的新项目中使用 neo4j-2.3.1 和 spring-data-neo4j 4。我将所有内容设置为 post (Spring Data Neo4j 4)。但是,我不能将任何数据存储到数据库中。 这是我的 neo4j 配置:

@Configuration
@EnableNeo4jRepositories("org.drexel.cas.repository")
@EnableTransactionManagement
@ComponentScan("org.drexel.cas")
public class RootConfig extends Neo4jConfiguration {
    @Override
    public SessionFactory getSessionFactory() {
        return new SessionFactory("org.drexel.cas.domain");
    }
    @Bean
    public Neo4jServer neo4jServer() {
        return new RemoteServer("http://localhost:7474", "neo4j", "xxxxxx");
    }
    @Override
    @Bean
    @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public Session getSession() throws Exception {
        return super.getSession();
    }
}

这是 REST api,它只是调用存储库来存储对象:

@Autowired
ProjectRepository repository;

@RequestMapping(value = "project", method = POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Project> create(@RequestBody Map<String, Object> param) {
    Project project = new Project(param);
    repository.save(project);
    return ResponseEntity.ok(project);
}

这是存储库:

@Repository
@Transactional
public interface ProjectRepository extends GraphRepository<Project> {

}

这是项目 class:

@NodeEntity
public class Project {
@GraphId
private Long id;
@Convert(MapJson.class)
private Map<String, Object> attributes;

public Project() {
    this.attributes = new HashMap<>();
}

public Project(Map<String, Object> params) {
    id = System.currentTimeMillis();
    this.attributes = params;
}

public boolean equals(Object object) {
    if (object == null)
        return false;
    if (!(object instanceof Project))
        return false;
    Project other = (Project) object;
    if (!other.getId().equals(this.id))
        return false;
    return true;
}

public int hashCode() {
    return this.id.hashCode();
}

}

这是转换器class:

public class MapJson implements AttributeConverter<Map<String, Object>, String> {

    @Override
    public String toGraphProperty(Map<String, Object> value) {
        Gson gson = new Gson();
                Type type = new TokenType<Map<String, Object>>(){}.getType();
                return gson.toJson(value, type);
           }

           @Override
           public Map<String, Object> toEntityAttribute(String value) {
                Gson gson = new Gson();
                Type type = new TokenType<Map<String, Object>>(){}.getType();
                return gson.fromJson(value, type);
           }

        }

我做了一个curl post之后,一切正常,没有任何异常和错误。然后我去 localhost:7474 get fetch all data,我什么也没得到。 这是日志,我做了一个 curl post 和 curl get。无一例外

    Dec 18, 2015 12:33:10 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
    WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:dummy' did not find a matching property.
    Dec 18, 2015 12:33:10 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Server version:        Apache Tomcat/8.0.29
    Dec 18, 2015 12:33:10 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Server built:          Nov 20 2015 09:18:00 UTC
    Dec 18, 2015 12:33:10 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Server number:         8.0.29.0
    Dec 18, 2015 12:33:10 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: OS Name:               Mac OS X
    Dec 18, 2015 12:33:10 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: OS Version:            10.11.2
    Dec 18, 2015 12:33:10 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Architecture:          x86_64
    Dec 18, 2015 12:33:10 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Java Home:             /Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre
    Dec 18, 2015 12:33:10 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: JVM Version:           1.8.0_65-b17
    Dec 18, 2015 12:33:10 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: JVM Vendor:            Oracle Corporation
    Dec 18, 2015 12:33:10 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: CATALINA_BASE:         /Users/bondwong/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp10
    Dec 18, 2015 12:33:10 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: CATALINA_HOME:         /Users/bondwong/apache-tomcat-8.0.29
    Dec 18, 2015 12:33:10 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Dcatalina.base=/Users/bondwong/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp10
    Dec 18, 2015 12:33:10 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Dcatalina.home=/Users/bondwong/apache-tomcat-8.0.29
    Dec 18, 2015 12:33:10 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Dwtp.deploy=/Users/bondwong/Documents/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp10/wtpwebapps
    Dec 18, 2015 12:33:10 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Djava.endorsed.dirs=/Users/bondwong/apache-tomcat-8.0.29/endorsed
    Dec 18, 2015 12:33:10 AM org.apache.catalina.startup.VersionLoggerListener log
    INFO: Command line argument: -Dfile.encoding=UTF-8
    Dec 18, 2015 12:33:10 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
    INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /Users/bondwong/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
    Dec 18, 2015 12:33:10 AM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler ["http-nio-8080"]
    Dec 18, 2015 12:33:10 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
    INFO: Using a shared selector for servlet write/read
    Dec 18, 2015 12:33:10 AM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
    Dec 18, 2015 12:33:10 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
    INFO: Using a shared selector for servlet write/read
    Dec 18, 2015 12:33:10 AM org.apache.catalina.startup.Catalina load
    INFO: Initialization processed in 866 ms
    Dec 18, 2015 12:33:10 AM org.apache.catalina.core.StandardService startInternal
    INFO: Starting service Catalina
    Dec 18, 2015 12:33:10 AM org.apache.catalina.core.StandardEngine startInternal
    INFO: Starting Servlet Engine: Apache Tomcat/8.0.29
    Dec 18, 2015 12:33:16 AM org.apache.jasper.servlet.TldScanner scanJars
    INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    Dec 18, 2015 12:33:16 AM org.apache.catalina.core.ApplicationContext log
    INFO: Spring WebApplicationInitializers detected on classpath: [org.drexel.cas.Initializer@28630d24]
    Dec 18, 2015 12:33:16 AM org.apache.catalina.core.ApplicationContext log
    INFO: Initializing Spring root WebApplicationContext
    00:33:16.781 [localhost-startStop-1] INFO  o.s.web.context.ContextLoader - Root WebApplicationContext: initialization started
    00:33:16.884 [localhost-startStop-1] INFO  o.s.w.c.s.AnnotationConfigWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Fri Dec 18 00:33:16 EST 2015]; root of context hierarchy
    00:33:16.972 [localhost-startStop-1] INFO  o.s.w.c.s.AnnotationConfigWebApplicationContext - Registering annotated classes: [class org.drexel.cas.RootConfig]
    00:33:17.435 [localhost-startStop-1] INFO  o.s.c.s.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'rootConfig' of type [class org.drexel.cas.RootConfig$$EnhancerBySpringCGLIB$dd11f8] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    00:33:17.439 [localhost-startStop-1] INFO  o.s.d.n.config.Neo4jConfiguration - Initialising PersistenceExceptionTranslationPostProcessor
    00:33:17.534 [localhost-startStop-1] INFO  o.s.d.n.config.Neo4jConfiguration - Initialising Neo4jSession
    00:33:18.031 [localhost-startStop-1] INFO  o.n.o.m.info.ClassFileProcessor - Starting Post-processing phase
    00:33:18.031 [localhost-startStop-1] INFO  o.n.o.m.info.ClassFileProcessor - Building annotation class map
    00:33:18.032 [localhost-startStop-1] INFO  o.n.o.m.info.ClassFileProcessor - Building interface class map for 2 classes
    00:33:18.032 [localhost-startStop-1] INFO  o.n.o.m.info.ClassFileProcessor - Registering default type converters...
    00:33:18.032 [localhost-startStop-1] INFO  o.n.o.m.info.ClassFileProcessor - Post-processing complete
    00:33:18.032 [localhost-startStop-1] INFO  o.n.o.m.info.ClassFileProcessor - 2 classes loaded in 15 milliseconds
    00:33:18.215 [localhost-startStop-1] INFO  o.s.d.n.mapping.Neo4jMappingContext - Neo4jMappingContext initialisation completed
    00:33:18.359 [localhost-startStop-1] INFO  o.s.d.n.config.Neo4jConfiguration - Initialising PersistenceExceptionTranslator
    00:33:18.364 [localhost-startStop-1] INFO  o.s.d.n.config.Neo4jConfiguration - Initialising PersistenceExceptionTranslationInterceptor
    00:33:18.368 [localhost-startStop-1] INFO  o.s.d.n.config.Neo4jConfiguration - Initialising Neo4jTransactionManager
    00:33:18.377 [localhost-startStop-1] INFO  o.s.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 1592 ms
    Dec 18, 2015 12:33:18 AM org.apache.catalina.core.ApplicationContext log
    INFO: Initializing Spring FrameworkServlet 'dispatcher'
    00:33:18.404 [localhost-startStop-1] INFO  o.s.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcher': initialization started
    00:33:18.407 [localhost-startStop-1] INFO  o.s.w.c.s.AnnotationConfigWebApplicationContext - Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Fri Dec 18 00:33:18 EST 2015]; parent: Root WebApplicationContext
    00:33:18.409 [localhost-startStop-1] INFO  o.s.w.c.s.AnnotationConfigWebApplicationContext - Registering annotated classes: [class org.drexel.cas.WebConfig]
    00:33:18.789 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/dummy/{id:\d+}]}" onto public org.springframework.http.ResponseEntity<org.drexel.cas.domain.Dummy> org.drexel.cas.web.DummyService.get(java.lang.Long)
    00:33:18.790 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/dummy],methods=[POST],consumes=[application/json],produces=[application/json]}" onto public org.springframework.http.ResponseEntity<org.drexel.cas.domain.Dummy> org.drexel.cas.web.DummyService.create(java.util.Map<java.lang.String, java.lang.Object>)
    00:33:18.814 [localhost-startStop-1] INFO  o.s.w.s.h.SimpleUrlHandlerMapping - Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler]
    00:33:18.851 [localhost-startStop-1] INFO  o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Fri Dec 18 00:33:18 EST 2015]; parent: Root WebApplicationContext
    00:33:18.938 [localhost-startStop-1] INFO  o.s.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcher': initialization completed in 534 ms
    Dec 18, 2015 12:33:18 AM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler ["http-nio-8080"]
    Dec 18, 2015 12:33:18 AM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler ["ajp-nio-8009"]
    Dec 18, 2015 12:33:18 AM org.apache.catalina.startup.Catalina start
    INFO: Server startup in 8483 ms
    create...
    get...

没有例外。 有谁知道如何修复它?

转换器不正确 - 它说的是从 Project 转换为 String 而不是 Map 转换为 String

已经有一个针对泛型问题的错误,所以在修复之前,您必须忽略它们并改用:

@Convert(MapJson.class)
private Map attributes;

使用属性转换器

public class MapJson implements AttributeConverter<Map,String> {

    @Override
    public String toGraphProperty(Map value) {
        //do something
    }

    @Override
    public Map<String, Object> toEntityAttribute(String value) {
        //do something
    }
}

尽量不要自己设置@GraphId属性的值

public Project(Map<String, Object> params) {
    //id = System.currentTimeMillis();
    this.attributes = params;
}