5

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.

1 Answer 1

2

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"
    
6
  • Thanks, Raphael. Regarding your first point, I use IntelliJ and I am pretty sure that the Hibernate plugin is removed when I commented that dependency out of BuildConfig.groovy. I believe that IntelliJ runs grails uninstall-plugin when you comment something out of BuildConfig.groovy. However, I will go back and double-check in the .grails directory that the Hibernate plug-in is indeed gone. I definitely did get rid of those 2 lines from BuildConfig.groovy that you mentioned. I did not know about the static mapWith="mongo", thank you for that information, I will try that. Aug 20, 2012 at 0:42
  • Did you also comment out the migration plugin? It appears that it depends on hibernate so it might be transiently reinstalling it. - github.com/grails-plugins/grails-database-migration/blob/master/…
    – Raphael
    Aug 20, 2012 at 1:06
  • Yes, I did remove the migration plugin. I believe that got added by default when creating app, but I definitely removed it. Thanks. Aug 20, 2012 at 1:21
  • Hi Raphael, Thanks very much for your recommendations. I upvoted your answer for being insightful, but I am still seeing the problems after trying the following: 1. Ran grails uninstall-plugin hibernate just to be sure. Got message | Warning No plugin [hibernate] installed, cannot uninstall (which is what I would expect). 2. Verified that BuildConfig.groovy did not have database-migration, hibernate plugins. 3. Added static mapWith = "mongo" to the Student domain class to explicitly specify it is persisted by MongoDB. Aug 20, 2012 at 15:04
  • Try to download and run this sample app github.com/xymor/grails-mongo-sample and compare with your own or share your code so I can help you debug it.
    – Raphael
    Aug 20, 2012 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.