MongoDB与Grails脚手架问题(不会发生在MySQL中) [英] MongoDB issues with Grails Scaffolding (do not occur in MySQL)

查看:111
本文介绍了MongoDB与Grails脚手架问题(不会发生在MySQL中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用MongoDB 2.0.6替换测试Grails 2.1 App的MySQL 5.5.25,并遇到一些奇怪的问题。



使用MongoDB而不是MySQL的问题:


  1. 脚手架,我无法通过使用静态约束来获得字段的顺序。

  2. 当我指定inList作为约束时,当使用MySQL后端时,我会得到一个下拉列表,但在使用MongoDB后端时是一个字段。
  3. 在指定了 blank = false 约束的字段上没有*(星号)。


    域类

    pre> 包学习

    班学生{

    字符串登录
    字符串名字
    字符串姓名
    字符串性别
    布尔活动

    日期dateCreated
    日期lastUpdated

    静态约束= {
    login()
    firstName(blank :false)
    lastName(空白:false)
    性别(inList:['M','F'])
    active()
    }

    }

    控制器

     包学习
    $ b $ class StudentController {

    def scaffold = true
    }

    DataSource.groovy (MySQL提供了注释):

      grails {
    mongo {
    host =dev-linux
    port = 27017
    username =study
    密码=********
    数据库eName =study
    }
    }

    // dataSource {
    // pooled = true
    // driverClassName =com.mysql.jdbc .Driver
    // dialect =org.hibernate.dialect.MySQL5InnoDBDialect
    // username =study
    // password =********
    // dbCreate =create-drop//创建'create','create-drop','update'
    // url =jdbc:mysql:// dev-linux:3306 /学习
    //
    //}
    // hibernate {
    // cache.use_second_level_cache = true
    // cache.use_query_cache = true
    / / cache.provider_class =net.sf.ehcache.hibernate.EhCacheProvider
    //}

    BuildConfig.groovy (显示的插件部分是我将MongoDB替换为MySQL的其余部分,此文件的其余部分是由Grails创建的默认文件)

      plugins {
    // build:hibernate:$ grailsVersion
    // compile:mysql-connectorj:5.1.12

    编译:mongodb:1.0.0.GA
    build:tomcat:$ grailsVersion


    我对MongoDB进行的唯一修改就是修改上面显示的 DataSource.groovy BuildConfig.groovy

    是否有我缺少的配置项?



    我的确看到有人提到这个 Nabble forum post 表示字段排序可能是MongoDB的一个问题。

    然而,这篇文章没有任何细节。

    另外,我不明白为什么后端数据库引擎会影响使用脚手架时渲染视图的方式。具体来说,在页面上的排序和下拉与文本字段。



    我认为这将来自Domain Class的字段类型和约束。



    有没有人遇到过在使用Grails + Scaffolding和MongoDB之前这种奇怪的行为?有没有人知道修补程序或有任何见解?



    非常感谢您提前,我很感激。

    解决方案

    使用MongoDB搭建脚手架的问题是,如果您只安装mongodb插件,grails将会看到模糊的域映射,并且会弹出类似这样的错误。你需要:


    • 像这样删除hibernate插件:

        grails uninstall-plugin hibernate 

      同样从BuildConfig中移除这些行.groovy:

        runtime:database-migration:1.1
      runtime:hibernate:$ grailsVersion


    • 显式地告诉Mongo通过添加以下行来保持给定的域:

       静态mapWith =mongo



    I tried using MongoDB 2.0.6 to replace MySQL 5.5.25 for a test Grails 2.1 App and am encountering some strange problems.

    Issues when using MongoDB but not MySQL:

    1. When using Scaffolding, I cannot get the fields to order by using static constraints

    2. When I specify inList as a constraint, I get a drop-down when using a MySQL backend, but a field when using a MongoDB backend.

    3. No * (asterisk) on fields where blank=false constraint specified.

    Domain Class:

    package study
    
    class Student {
    
        String login
        String firstName
        String lastName
        String gender
        Boolean active
    
        Date dateCreated
        Date lastUpdated
    
        static constraints = {
            login()
            firstName(blank: false)
            lastName(blank: false)
            gender(inList: ['M', 'F'])
            active()
        }
    
    }
    

    Controller

    package study
    
    class StudentController {
    
        def scaffold = true
    }
    

    DataSource.groovy (MySQL stuff commented out):

    grails {
      mongo {
        host = "dev-linux"
        port = 27017
        username = "study"
        password= "********"
        databaseName = "study"
      }
    }
    
    //dataSource {
    //    pooled = true
    //    driverClassName = "com.mysql.jdbc.Driver"
    //    dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
    //    username = "study"
    //    password = "********"
    //    dbCreate = "create-drop" // one of 'create', 'create-drop','update'
    //    url = "jdbc:mysql://dev-linux:3306/study"
    //
    //}
    //hibernate {
    //    cache.use_second_level_cache = true
    //    cache.use_query_cache = true
    //    cache.provider_class = "net.sf.ehcache.hibernate.EhCacheProvider"
    //}
    

    BuildConfig.groovy (plugins section shown was all I changed to put MongoDB in place of MySQL, the remainder of this file is the default created by Grails)

    plugins {
          // build ":hibernate:$grailsVersion"
          // compile ":mysql-connectorj:5.1.12"
    
          compile ":mongodb:1.0.0.GA"
          build ":tomcat:$grailsVersion"
    
    }
    

    The only changes I made to put in MongoDB and take out MySQL is the changes to the DataSource.groovy and BuildConfig.groovy shown above.

    Is there any configuration item that I am missing?

    I did see someone mention on this Nabble forum post that the field ordering may be an issue with MongoDB.

    However, this post did not have any details.

    Also, I did not understand why or how the back end Database engine could impact how the view is rendered when using scaffolding. Specifically, the ordering on a page and drop-down vs textfield.

    I would have thought that would come from the Domain Class's field types and constraints.

    Has anyone come across this odd behavior when using Grails+Scaffolding with MongoDB before? Does anyone know of a fix or have any insight?

    Thank you very much in advance, I appreciate it.

    解决方案

    Scaffolding with MongoDB works, the problem is if you just install mongodb plugin, grails will see ambiguous domain mappings and errors like these pop up. You need to either:

    • Remove hibernate plugin like this:

      grails uninstall-plugin hibernate
      

      Also remove these lines from BuildConfig.groovy:

      runtime ":database-migration:1.1"
      runtime ":hibernate:$grailsVersion"
      

    • Explicitly tell a given domain is persisted by Mongo by adding this line to it:

      static mapWith="mongo"
      

    这篇关于MongoDB与Grails脚手架问题(不会发生在MySQL中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆