2

I have a Grails 1.3.7 domain class that is declared like this:

class Document {
    String url

    Map metadata = new HashMap()

    static constraints = {
        url(nullable:false, blank: false, maxSize: 2048)
        metadata()
    }
}

The schema that is generated looks like this:

+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| url         | longtext     | NO   |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+

+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| metadata     | bigint(20)   | YES  |     | NULL    |       |
| metadata_idx | varchar(255) | YES  |     | NULL    |       |
| metadata_elt | varchar(255) | NO   |     | NULL    |       |
+--------------+--------------+------+-----+---------+-------+

I am wondering how I can specify different types (specifically, different size) for the document_metadata table. I would like to be able to store strings longer than 255 characters. I could not find any relevant documentation online, possibly because I couldn't come up with any good keywords. Map and Collection are pretty generic terms!

Thanks,

Gene

Gene Golovchinsky
  • 6,101
  • 7
  • 53
  • 81

2 Answers2

2

The only way you are going to be able to influence the DDL for your "metadata" is to make it a concrete domain class. Using HashMap isn't going to give you the ability to do so.

Joshua Moore
  • 24,706
  • 6
  • 50
  • 73
  • Darn! I was hoping that there were some magical attributes that I could specify on the mapping to influence the way the `document_metadata` table is declared. – Gene Golovchinsky Sep 04 '11 at 20:07
  • The only "magic" you could do would be to involve a custom handler for Hibernate for the HashMap type, however, that would apply to ALL hash maps (including those used by GORM to manage relationships). – Joshua Moore Sep 04 '11 at 20:12
2

It would be quite easy just to run an sql alter command to database. A good place for this alter command would be BootStrap and with some logic that checks the table column definitions wheter they're ok or not. Grails won't touch already created columns.

heikkim
  • 2,955
  • 2
  • 24
  • 34
  • 1
    This problem just cropped up again for me, and your solution worked like a charm when I did a manual `ALTER TABLE` command; not quite sure how to implement this in Bootstrap in a DB-independent manner. – Gene Golovchinsky Jun 08 '12 at 03:34