Objectless Blog

Grails boolean saved as BIT

by steve on Dec.30, 2009, under Programming

Depending on the Grails version booleans  are stored differently; either as INT or BIT.   BIT(1) is a special datatype made just for booleans.  However old dogs prefer storing booleans as small int.  In order to do this, you have to create a customized Hibernate dialect.
MySQL Example:  foo.bar.brewsoftcorp.CustomMySQL5InnoDBDialect.groovy
package foo.bar.brewsoftcorp
import org.hibernate.dialect.MySQL5InnoDBDialect
import java.sql.Types
class CustomMySQL5InnoDBDialect extends MySQL5InnoDBDialect {
CustomMySQL5InnoDBDialect() {
registerColumnType(Types.BIT, ‘boolean’)
}
}
package foo.bar.brewsoftcorp
import org.hibernate.dialect.MySQL5InnoDBDialect
import java.sql.Types
class CustomMySQL5InnoDBDialect extends MySQL5InnoDBDialect {
CustomMySQL5InnoDBDialect () {
registerColumnType(Types.BIT, ‘boolean’)
}
}
SQL2000/2005 Example: foo.bar.brewsoftcorp.CustomSQLServerDBDialect.groovy
package foo.bar.brewsoftcorp
import org.hibernate.dialect.SQLServerDialect
import java.sql.Types
public class CustomSQLServerDBDialect extends SQLServerDialect {
public CustomSQLServerDBDialect() {
registerColumnType(Types.BIT, ‘boolean’)
}
}
Then add this line to your DataSource.groovy
dialect = util.CustomMySQL5InnoDBDialect //for MySQL
##############################
dataSource {
pooled = true
driverClassName = “com.mysql.jdbc.Driver”
username = “username”
password = “password”
dialect = util.CustomMySQL5InnoDBDialect
}
##############################

Depending on the Grails version booleans  are stored differently; either as INT or BIT.   BIT(1) is a special datatype made just for booleans.  However old dogs prefer storing booleans as small int.  In order to do this, you have to create a customized Hibernate dialect.

MySQL Example:  foo.bar.brewsoftcorp.CustomMySQL5InnoDBDialect.groovy

package foo.bar.brewsoftcorp
import org.hibernate.dialect.MySQL5InnoDBDialect
import java.sql.Types
class CustomMySQL5InnoDBDialect extends MySQL5InnoDBDialect {

CustomMySQL5InnoDBDialect() {
registerColumnType(Types.BIT, ‘boolean’)
}
}

SQL2000/2005 Example: foo.bar.brewsoftcorp.CustomSQLServerDBDialect.groovy

package foo.bar.brewsoftcorp
import org.hibernate.dialect.SQLServerDialect
import java.sql.Types

public class CustomSQLServerDBDialect extends SQLServerDialect {
public CustomSQLServerDBDialect() {
registerColumnType(Types.BIT, ‘boolean’)
}
}

Then add this line to your DataSource.groovy
dialect = util.CustomMySQL5InnoDBDialect //for MySQL

##############################

dataSource {
pooled = true
driverClassName = “com.mysql.jdbc.Driver”
username = “username”
password = “password”
dialect = util.CustomMySQL5InnoDBDialect
}

##############################


Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...