Friday, July 18, 2008

Validate legacy databases with Grails

By default Grails assume you are creating a new database schema from scratch. But one of its strengths is it's ease of mapping to existing database tables. However when you map to existing tables you must be careful. By default the DataSource.groovy file, is configured to create-drop tables so it is easy to inadvertently wipe out data and tables. In addition switching to the less disruptive option of update may not be safe either. See GORM objects by default have an id and version attribute that are injected by GORM. When GORM gets initialized it will add id and version columns to your database which you may not want or expect. So the best option may be to use the validate option which is not documented in the Grails user guide. On start up it will validate the tables have the columns that your domain classes expect and if not fail to start.

By the way, to turn off the version column you will need to set version to false in the domain class mappings. To prevent the id column from being created, you will need to map id to the primary key column of your table.

7 comments:

BJ Allmon said...

Example:

class MyLegacyObjectMapping {

static mapping = {
table 'SOME_LEGACY_TABLE'

firstName = column:'FIRST_NAME'
lastName = column:'LAST_NAME'

columns {
id column:'MY_LEGACY_ID_FIELD'
}

version false
}

String firstName
String lastName
}

Christopher M Judd said...

Thanks BJ.

Anonymous said...

Hello,
I'm trying to do the same as this example, but a Java.lang.NullPointerException on my list.gsp exist each time i compile, and id and version exist also in my database Oracle.
All i want is to replace id by my own primary key in my DB and delete version also.
and some times he's asking for an insert="false" and update="false" !!!
Please if u can help me, thank you..

Christopher M Judd said...

Georges, can you provide me with a call stack for the NullPointerException? Also, what version of Grails are you using?

Anonymous said...

Hello Christopher,

I'm using Grails 1.0.4
Stack Trace:
java.lang.NullPointerException
at Z__georges_grails_app_views_georges_list_gsp$_run_closure15.doCall(Z__georges_grails_app_views_georges_list_gsp:92)
at Z__georges_grails_app_views_georges_list_gsp.run(Z__georges_grails_app_views_georges_list_gsp:88)
This is what i'm getting!!
I tried to change list.gsp and show.gsp but always the same error.
what are the changes that we must do, while doing a mapping from id to another primarykey? changing the controller? the gsp? the domain class of course.
I think that my problem is from the list.gsp where the call is not good(what are the changes that we can do for the georgescontroller.groovy to obtain a good mapping?)
Thank you for your help in advance.

Regards,
Georges

Anonymous said...

Thanks Chris, I found my errors, and it works !!

Chee Yuan said...

Hi.. George... mind to share the solution for your problem?

AWS EC2 Hibernate Java SDK v2 Example

I recently wanted to automate the creation of developer VMs in AWS using EC2 instances. To improve the developer experience (DX), I didn...