Search code examples
mysqlhibernategrailsgrails-ormgrails-domain-class

How to configure a MySQL DataSource on Grails 3.1.x with web-api profile?


I already use Grails since v1.3.x and now I'm not having success with the DataSource/GORM configuration...

My steps were:
1 - $ grails create-app myApi --profile=web-api.

2 - Configure the application.yml file with:

dataSource:
    pooled: true
    jmxExport: true
    driverClassName: com.mysql.jdbc.Driver
    dialect: org.hibernate.dialect.MySQL5InnoDBDialect
    username: root
    password:

environments:
    development:
        dataSource:
            dbCreate: update
            url: jdbc:mysql://localhost:3306/test-app

3 - On build.gradle file, replace the runtime dependency "com.h2database:h2" with "mysql:mysql-connector-java:5.1.37"

4 - Create a database called test-app

5 - Create a domain: $grails create-domain-class MyDomain

6 - Add a String attribute on MyDomain classe called name
Example:

package myapi

class MyDomain {
    String name
    static constraints = { }
}

7 - Run the app: $grails run-app
Output:

Running application...
objc[27851]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/bin/java and /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.
Grails application running at http://localhost:8080 in environment: development

And no tables were created... anyone knows what step I'm missing in here?

OBS.: Even if I misspelled the database name, the app still running and no error happens.


Solution

  • Hibernate plugin are not include by default as a normal dependency on Grails 3.1.0.M2 with web-api profile, so in order to fix this you just have to add in your dependencies:

    runtime "org.grails.plugins:hibernate:4.3.10.7"
    

    More info at: https://github.com/grails/grails-core/issues/9366