I’m started a project using Grails, and I wanted a way to get Grails to generate the Domain classes from an existing SQLite database.  The tool I found to do this was the Grails Database Reverse Engineering Plugin.  This takes an existing SQLite database and gives a groovy Domain class file for each database table that exists.

Settings in BuildConfig.groovy

In the plugins section, add the line

runtime ':db-reverse-engineer:0.5'

Settings in Config.groovy

Add this configuration option (in the global scope) and replace the package name with whatever package name you want.

grails.plugin.reveng.packageName = 'com.somepackagename'

Settings in DataSource.groovy

Under environments -> development, set the following:

dataSource {
     dbCreate = ""
     url = "jdbc:sqlite:/path/to/database/file"
     dialect="hibernate.SQLiteDialect"
     driverClassName = "org.sqlite.JDBC"
     readOnly="true"
}

Get the SQLite Dialect

Download HibernateSQLite_with_jar.rar and then extract SQLiteDialect.java from the src/main/java/dialect directory to your applications hibernate directory (/grails-app/conf/hibernate). Also:

  1. Change the package name to hibernate
  2. Add import for org.hibernate.type.StringType
  3. Add import for org.hibernate.type.IntegerType
  4. Change all instances of Hibernate.STRING to StringType.INSTANCE
  5. Change all instances of Hibernate.INTEGER to IntegerType.INSTANCE

Add the SQLite JDBC Library to the Project

The SQLite JDBC Library can be downloaded here: https://bitbucket.org/xerial/sqlite-jdbc/downloads/sqlite-jdbc-3.7.2.jar and then place it in the “lib” folder of the project

Add jta-1.1.jar to the Project

This library came with the Grails installation.  Place it in the “lib” folder of the project.

Running the Plugin

Calling the following will generate the files:

db-reverse-engineer

Result

I now have one groovy Domain class for each table in the database in my Grails project!

Caveat

The unique constraints that got written into my Domain classes were wrong.  I had to fix them.  Initially, the unique identifier wrote the name of the database column, but instead that should be changed to the variable name that the Domain class called it.

Also, the hasMany and belongTo relationships are not set by the plugin (at least I did not configure it), although there should be a way to configure it with the plugin.