【问题标题】:Neo4j database for Grails app without using Neo4j GORM plugin不使用 Neo4j GORM 插件的 Grails 应用程序的 Neo4j 数据库
【发布时间】:2015-01-02 15:13:20
【问题描述】:

我想制作一个使用 Grails 框架作为服务器部分和 Neo4j 数据库作为存储的 Web 应用程序。我在 Grails 站点上找到了 Neo4j GORM 插件,但不幸的是,经过多次尝试,我无法使其正常工作。如果我解决了一个错误,我会得到更多,我什至无法设置一个基本的应用程序来工作。所以我想编写自己的代码来处理 CRUD 操作。我想覆盖域类的默认 CRUD 操作。例如,当我保存一个 Person 实体时,我会使用 Neo4j 数据库提供的 REST API 将实体存储在那里等。这是一个可接受的解决方案吗?如果是,那么我应该如何继续覆盖域类的 CRUD 方法?

我在使用 GORM 插件时遇到的问题:我按照文档中描述的步骤操作:http://projects.spring.io/grails-data-mapping/neo4j/manual/guide/gettingStarted.html。我创建了一个简单的 Grails Web 应用程序,包括 Neo4j-GORM 插件依赖项。应用程序启动。

如果我尝试访问“org.grails.datastore.gorm.neo4j.Neo4jController”,我会收到一些奇怪的错误:

这是我在控制台中遇到的错误:

015-01-04 14:11:22,460 [http-bio-8080-exec-2] ERROR errors.GrailsExceptionResolver  - NullPointerException occurred when processing request: [GET] /ANewHope/neo4j/node
Cannot invoke method getNodeById() on null object. Stacktrace follows:
Message: Cannot invoke method getNodeById() on null object
    Line | Method
->>   30 | doCall    in org.grails.datastore.gorm.neo4j.Neo4jController$_closure2
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    198 | doFilter  in grails.plugin.cache.web.filter.PageFragmentCachingFilter
|     63 | doFilter  in grails.plugin.cache.web.filter.AbstractFilter
|   1145 | runWorker in java.util.concurrent.ThreadPoolExecutor
|    615 | run . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^    745 | run       in java.lang.Thread

这是我的 BuildConfig.groovy:

grails.servlet.version = "3.0" // Change depending on target container compliance (2.5 or 3.0)
grails.project.class.dir = "target/classes"
grails.project.test.class.dir = "target/test-classes"
grails.project.test.reports.dir = "target/test-reports"
grails.project.work.dir = "target/work"
grails.project.target.level = 1.6
grails.project.source.level = 1.6
//grails.project.war.file = "target/${appName}-${appVersion}.war"

grails.project.fork = [
    // configure settings for compilation JVM, note that if you alter the Groovy version forked compilation is required
    //  compile: [maxMemory: 256, minMemory: 64, debug: false, maxPerm: 256, daemon:true],

    // configure settings for the test-app JVM, uses the daemon by default
    test: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, daemon:true],
    // configure settings for the run-app JVM
    run: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, forkReserve:false],
    // configure settings for the run-war JVM
    war: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256, forkReserve:false],
    // configure settings for the Console UI JVM
    console: [maxMemory: 768, minMemory: 64, debug: false, maxPerm: 256]
]

grails.project.dependency.resolver = "maven" // or ivy
grails.project.dependency.resolution = {
    // inherit Grails' default dependencies
    inherits("global") {
        // specify dependency exclusions here; for example, uncomment this to disable ehcache:
        // excludes 'ehcache'
    }
    log "error" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose'
    checksums true // Whether to verify checksums on resolve
    legacyResolve false // whether to do a secondary resolve on plugin installation, not advised and here for backwards compatibility

    repositories {
        inherits true // Whether to inherit repository definitions from plugins

        grailsPlugins()
        grailsHome()
        mavenLocal()
        grailsCentral()
        mavenCentral()
        // uncomment these (or add new ones) to enable remote dependency resolution from public Maven repositories
        //mavenRepo "http://repository.codehaus.org"
        //mavenRepo "http://download.java.net/maven/2/"
        //mavenRepo "http://repository.jboss.com/maven2/"
        mavenRepo 'http://m2.neo4j.org/releases'
    }

    dependencies {
        // specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes e.g.
        // runtime 'mysql:mysql-connector-java:5.1.29'
        // runtime 'org.postgresql:postgresql:9.3-1101-jdbc41'

        test "org.grails:grails-datastore-test-support:1.0.2-grails-2.4"
    }

    plugins {
        // plugins for the build system only
        build ":tomcat:7.0.55"

        // plugins for the compile step

        compile ":neo4j:2.0.0-M02"
        compile ":scaffolding:2.1.2"
        compile ':cache:1.1.8'
        compile ":asset-pipeline:1.9.9"


        // plugins needed at runtime but not for compilation
       // runtime ":hibernate4:4.3.6.1" // or ":hibernate:3.6.10.18"
        //runtime ":database-migration:1.4.0"
        runtime ":jquery:1.11.1"

        // Uncomment these to enable additional asset-pipeline capabilities
        //compile ":sass-asset-pipeline:1.9.0"
        //compile ":less-asset-pipeline:1.10.0"
        //compile ":coffee-asset-pipeline:1.8.0"
        //compile ":handlebars-asset-pipeline:1.3.0.3"
    }
}

这里是 DataSource.groovy:

dataSource {
    pooled = true
    jmxExport = true
    driverClassName = "org.h2.Driver"
    username = "sa"
    password = ""
}
//hibernate {
//    cache.use_second_level_cache = true
//    cache.use_query_cache = false
////    cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' // Hibernate 3
//    cache.region.factory_class = 'org.hibernate.cache.ehcache.EhCacheRegionFactory' // Hibernate 4
//    singleSession = true // configure OSIV singleSession mode
//    flush.mode = 'manual' // OSIV session flush mode outside of transactional context
//}

//grails {
//    neo4j {
//        type = "embedded"
//        location = "C:\\data\\neo4j"
//        params = []
//    }
//}

// environment specific settings
environments {
    development {
        dataSource {
            dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
            url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
        }
    }
    test {
        dataSource {
            dbCreate = "update"
            url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
        }
    }
    production {
        dataSource {
            dbCreate = "update"
            url = "jdbc:h2:prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
            properties {
               // See http://grails.org/doc/latest/guide/conf.html#dataSource for documentation
               jmxEnabled = true
               initialSize = 5
               maxActive = 50
               minIdle = 5
               maxIdle = 25
               maxWait = 10000
               maxAge = 10 * 60000
               timeBetweenEvictionRunsMillis = 5000
               minEvictableIdleTimeMillis = 60000
               validationQuery = "SELECT 1"
               validationQueryTimeout = 3
               validationInterval = 15000
               testOnBorrow = true
               testWhileIdle = true
               testOnReturn = false
               jdbcInterceptors = "ConnectionState"
               defaultTransactionIsolation = java.sql.Connection.TRANSACTION_READ_COMMITTED
            }
        }
    }
}

在此之后,我尝试按照文档中的建议指定位置,并将以下内容添加到 DataSource.groovy:

grails {
    neo4j {
        type = "embedded"
        location = "C:\\data\\neo4j"
        params = []
    }
}

结果是一样的。在尝试失败后,我决定尝试其他解决方案。 我想在我的学士论文项目中使用 Neo4j,我的教授建议我将它与 Grails 一起使用。我喜欢 Neo4j 和 Grails,但不幸的是,当我尝试将它们一起使用时遇到了这个问题。

【问题讨论】:

  • 究竟有什么不适合您?使用 GORM 插件?作者一定会喜欢你的反馈。

标签: grails neo4j grails-orm crud


【解决方案1】:

如果您不想使用 GORM 插件,您可以使用 Grails 中的 Neo4j-JDBC 驱动程序将参数化的 Cypher 语句发送到服务器。就像您对关系数据库所做的那样。

还有一个新的 ORM 正在开发中,但仍在进行中。你可以在这里找到它:https://github.com/neo4j/neo4j-ogm

【讨论】:

    【解决方案2】:

    虽然 GORM 功能强大,但它并不适合每个项目。我经常选择使用其他东西,但是,grails 预计domain 目录中的任何东西都将使用 GORM。如果你想使用不同的 API,你应该把你的类放在src/groovy 中。如果您仍然希望能够使用约束进行验证,可以将@Validatable 注释应用于该类。可以在Validation Grails 文档中找到有关这方面的详细信息。然后,我会将与 Neo4j REST API 接口的所有代码放入 Grails 服务中。

    【讨论】:

      猜你喜欢
      • 2014-05-11
      • 2015-02-28
      • 2014-08-30
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多