Skip to content

Instantly share code, notes, and snippets.

@mrapczynski
Last active December 17, 2018 17:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrapczynski/5479548 to your computer and use it in GitHub Desktop.
Save mrapczynski/5479548 to your computer and use it in GitHub Desktop.
Example of creating a Hibernate configuration class for the Grails platform to allow developer control which tables (via domain objects) are given the GORM treatment during startup of a Grails application. I created it to be simple object-oriented method where I can mix domain objects that were either (a) custom to the application and needed sch…
package edu.fhda.grails.hibernate
import groovy.util.logging.Log4j
import org.codehaus.groovy.grails.commons.GrailsApplication
import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsAnnotationConfiguration
import org.hibernate.HibernateException
import org.hibernate.dialect.Dialect
import org.hibernate.tool.hbm2ddl.DatabaseMetadata
/**
* Customized Hibernate configurator that extends the default GrailsAnnotationConfiguration to provide
* a method for specifying a configurable list of tables to ignore during Grails startup. Very helpful
* if you want to have domain objects that should be GORM managed alongside domain objects that
* map to existing Banner database tables. This will prevent Grails from trying to create, update, or drop
* Banner baseline schema objects.
*/
@Log4j
class BannerHibernateConfiguration extends GrailsAnnotationConfiguration {
private GrailsApplication grailsApplication
@Override
String[] generateDropSchemaScript(Dialect dialect) throws HibernateException {
return checkAndRemoveIgnoredTables(super.generateDropSchemaScript(dialect));
}
@Override
String[] generateSchemaCreationScript(Dialect dialect) throws HibernateException {
return checkAndRemoveIgnoredTables(super.generateSchemaCreationScript(dialect));
}
@Override
String[] generateSchemaUpdateScript(Dialect dialect, DatabaseMetadata databaseMetadata) throws HibernateException {
return checkAndRemoveIgnoredTables(super.generateSchemaUpdateScript(dialect, databaseMetadata));
}
private String[] checkAndRemoveIgnoredTables(String[] sqlStatements) {
// Create a collection of SQL statements for non-ignored tables
ArrayList<String> validSqlStatements = new ArrayList<>()
// Get the list of table names to ignored from the application config
def ignoredTables = grailsApplication.config.edu.fhda.grails.hibernate.banner.ignoreTables as List<String>
if(ignoredTables?.size() > 0) {
log.debug "Using ignored tables configuration: ${ignoredTables}"
// Iterate each SQL DML statement generated by GORM
sqlStatements?.each { String sqlStatement ->
boolean safeToAdd = true
// Check to see if the statement references any ignored tables
ignoredTables?.each { String tableName ->
if(sqlStatement.toLowerCase().contains("table ${tableName.toLowerCase()}")) {
/* NOTE: Log reference using this keyword prevents NullPointerExceptions */
this.log.debug "Bypassing ${tableName} for Grails domain mapping"
safeToAdd = false
}
}
// Did the statement pass the test?
if(safeToAdd) {
validSqlStatements.add(sqlStatement)
}
}
// Return a String array of validated statements
return validSqlStatements.toArray() as String[]
}
return sqlStatements
}
@Override
void setGrailsApplication(GrailsApplication application) {
// Run superclass method
super.setGrailsApplication(application)
// Inject Grails application reference
this.grailsApplication = application
}
}
// Hibernate Configuration for Banner (one or more tables Grails should never try to modify)
edu.fhda.grails.hibernate.banner.ignoreTables = [
"STVTERM"
]
// Example data source configuration
dataSource {
pooled = true
driverClassName = "oracle.jdbc.OracleDriver"
configClass = edu.fhda.grails.hibernate.BannerHibernateConfiguration
}
@ayago
Copy link

ayago commented Jan 23, 2014

It would be nice if a domain table could be mark so that it could be included in the list of tables that should not be modified. Sample would be static mapping = {includeInBanner : true}

@alex-shamshurin
Copy link

Replace "if" condition with this please

if (sqlStatement.toLowerCase().contains("table ${tableName.toLowerCase()}")
|| sqlStatement.toLowerCase().contains("table if exists ${tableName.toLowerCase()}")) {...}

or tables will be deleted anyway or use regexp

@1442acb42ee0e3a727d7330e9755eb94

In the latest GORM Version 6.1 GrailsAnnotationConfiguration is not available anymore. See http://gorm.grails.org/latest/hibernate/api/org/grails/orm/hibernate/cfg/package-summary.html

Which class from the summary replaces GrailsAnnotationConfiguration an canbe use in 6.1 to implement the same behaviour?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment