Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to adjust constraints / DB mapping for Map within grails domain class

Following grails domain class:

class MyClass {
  Map myMap
}

Now for myMap, grails automatically creates a new table for the elements in the map. However if I add elements which are too long (e.g. 1024 characters), I get a DB error.

Can I somehow tell grails to make the respective column in myMap's table big enough to allow for larger Strings, or do I have to do this manually in the DB?

I already tried

static constraints = {
  myMap(maxSize:1024)
}

which doesn't work (as expected because maxSize should refer to the Map's values and not to the Map itself).

If not via constraints, maybe there's a way to do it via

static mapping { ... }

?

like image 456
Jörg Brenninkmeyer Avatar asked Jul 13 '10 13:07

Jörg Brenninkmeyer


People also ask

What is static mapping in Grails?

The mapping static property configures how GORM maps the domain class to the database. See the section on the ORM DSL in the user guide for more information.

What are Grails constraints?

Constraints provide Grails with a declarative DSL for defining validation rules, schema generation and CRUD generation meta data. For example, consider these constraints: class User { ... static constraints = { login size: 5.. 15, blank: false, unique: true password size: 5..

What is Grails domain class?

A domain class fulfills the M in the Model View Controller (MVC) pattern and represents a persistent entity that is mapped onto an underlying database table. In Grails a domain is a class that lives in the grails-app/domain directory.


1 Answers

An alternative approach I used successfully was to push the map out into a collection of a collaborator domain class.

class DynaProperty {
    String name
    String value

    static belongsTo = MyClass
    static constraints = {
        value(maxSize:4000)  //Or whatever number is appropriate
    }
}

And then in MyClass:

class MyClass {
    static hasMany = [dynaProperties:DynaProperty]
}

This is almost a map, and it gives you the ability to use dynamic finders to pull up an individual entry.

like image 92
Matt Lachman Avatar answered Sep 24 '22 17:09

Matt Lachman