Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB issues with Grails Scaffolding (do not occur in MySQL)

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.

like image 492
Philip Tenn Avatar asked Nov 03 '22 17:11

Philip Tenn


1 Answers

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"
    
like image 60
Raphael Avatar answered Nov 08 '22 05:11

Raphael