Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing DB Without Domain Classes With Grails

I've ran into an issue while trying to put together a Grails app with an AS400/DB2 database. I cannot get most of the files mapped because they do not have a unique field to use as an id. And even if they do they are a text based field and not in a format that could be converted to a long type. (I don't get why the PK has to be a long data type? If you wanted to us a sequence or AI for the pk that would make sense but what if you just needed a unique key? Am I missing something here?)

I'm wondering if it is possible to keep the datasource that I have set up and just use it for straight SQL access to the DB without having to use domain objects?

Something I've seen was setting the domain object as transient. But I don't know if you could still do something like that without an id field. Anybody know how that works?

Any ideas?

Thanks, Jon

like image 993
jonsinfinity Avatar asked Dec 09 '10 04:12

jonsinfinity


1 Answers

You can access the database quite easily, we are doing the same in certain cases for performance reasons:

class SomeService {
    def dataSource;

    def nativeAccessMethod = {
        def sql = new Sql(dataSource);
        def rows = sql.rows("select * from myTable");
        /* processing continues ...*/
    }
}

Groovy's native SQL support is also nice.

like image 129
Gregor Petrin Avatar answered Sep 25 '22 00:09

Gregor Petrin