Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

map existing mysql database with gorm

I'm trying to map a (small part of a) Joomla MySQL database using GORM with Grails 2.0.

I'm reading a book on the argument (Grails) and googling the web for tech article, but I still need a good reference to map Groovy/Java types to MySQL fields.

I'm starting with a simple table jos_bannerclient.

class BannerClient {
    String name
    String contact
    String email
    String notes
    String editor = ''

    static constraints = {
        name(blank:false)
        contact(nullable:true)
        email(nullable:true)
        notes(nullable:true)
        editor(nullable:true)
    }

    static mapping = {
        datasource 'joomla'
        table 'jos_bannerclient'
        id column:'cid', type:'int'
        notes column:'extrainfo', type:'text'
        version false
    }
}

At this point the record is generated in the database but if I save the domain with failOnError:true I get this error: java.lang.IllegalArgumentException.

I've problems mapping the checked_out TINYINT field. The only thing for GORM to validate that field is to declare it as Boolean, why it doen't work with Byte?

I've also some doubt on how to map a MySQL TIME field like checked_out_time.

I've also read some part of Hibernate documentation, but still not gaining the needed knowledge to accomplish this task!

Anyone can help please?

like image 409
gsscoder Avatar asked Dec 30 '11 09:12

gsscoder


People also ask

What databases does Gorm support?

GORM provides official support for sqlite , mysql , postgres , sqlserver . Some databases may be compatible with the mysql or postgres dialect, in which case you could just use the dialect for those databases. For others, you can create a new driver, it needs to implement the dialect interface.

What is ORM in Golang?

Golang ORMs Luckily, the Go community has built a number of Object Relational Mapping libraries (ORMs) to allow Go developers to use JSON key:value pair syntax and encoding to map directly to a SQL database like PostgreSQL. ORMs enable developers to use their native programming paradigm to map data to SQL.

What is Gorm?

Gorm is an Irish (Gaelic) word meaning "blue". Ignorance is bliss => Ignorance means not having the blues. => Ignorance = Gormless.

What is Gorm db?

GORM provides First , Take , Last methods to retrieve a single object from the database, it adds LIMIT 1 condition when querying the database, and it will return the error ErrRecordNotFound if no record is found. // Get the first record ordered by primary key. db.First(&user)


1 Answers

You are indicating "type" but should be indicating "sqlType", which is why I believe you are having issues with TINYINT and having to use a Boolean instead of Byte. Id is an int (well actually bigint) by default, but it won't complain at you about that unless you are using dbCreate = "validate" and the other values are strings so that won't necessarily give you an issue with compatibility for notes.

static mapping = {
    datasource 'joomla'
    table 'jos_bannerclient'
    id column:'cid', sqlType:'int'
    notes column:'extrainfo', sqlType:'text'
    version false
}

As for the TIME issue, I have been able to specify type TIMESTAMP without a problem, so I can imagine TIME will work fine as well. All SQL types should be supported. For example:

static mapping = {
  dateCreated sqlType: 'TIMESTAMP', defaultValue: 'CURRENT_TIMESTAMP'
}
like image 120
th3morg Avatar answered Sep 28 '22 01:09

th3morg