2

By default when you create a domain class, it will automatically add "id" and "version" column for all the domain classes (tables). What if I want to add a column say for e.g. "isChecked" and this should be added automatically to all the domain classes (i.e tables) similar way "id" and "version" columns are added. How can I achieve this and also if I don't want to have "isChecked" for a specific domain class, I should also be able to do that.

How can I do this in Grail 1.3.7?

Thank You. Jay Chandran

Edit: Can I get more inputs? Suggested answers did not work!

Jay Chandran
  • 382
  • 1
  • 8
  • 20

3 Answers3

3

You could use the meta programming magic that Groovy provides for this sort of thing, however, I would probably just go a more typical route and use inheritance. Create a parent domain that contains isChecked (and anything else you need) and have your domains that require them extend that class.

Gregg
  • 34,973
  • 19
  • 109
  • 214
  • And you'll most likely want to disable the table-per-hierarchy scheme in favor of the table-per-subclass scheme in the ```mapping``` closure of the parent class. – codelark Aug 08 '11 at 15:07
  • @Jay Chandran There is an example in the [GORM docs](http://grails.org/doc/latest/guide/single.html#5.5.2%20Custom%20ORM%20Mapping). Section 5.5.2.3. – codelark Aug 08 '11 at 15:26
  • @codelark I have a ParentDomain class with isChecked and a ChildDomain class which extends the parent domain class and has field such as first_name. I have added tablePerHierarchy false, and when I see in the database, I see parent_domain table with columns "id", "version" & "is_checked" and child_domain table with columns "id" and "first_name" columns. "is_checked" column was not created in "child_domain" table. Why is that so? – Jay Chandran Aug 08 '11 at 15:54
  • 3
    Put the base class in src/groovy so Hibernate inheritance doesn't come into play. This way you're just using regular Java/Groovy inheritance and sharing fields/getters/setters. – Burt Beckwith Aug 08 '11 at 17:14
  • @Burt Beckwith But still the child_domain table does not have the "is_checked" column. Am I missing something? – Jay Chandran Aug 09 '11 at 11:52
  • putting the BaseClass in src/groovy did not work for me because some O2M association in the childClass ended in a "table not found exception". – nils petersohn Dec 07 '11 at 10:47
0

I would recommend creating a plugin that modifies (adds the property) your domain artifacts. You can read more about plugins and artifacts. You could then easily add a static property (e.g. static nochecked = true) to filter out domain artifacts you don't want to add the new property to as you see fit.

Joshua Moore
  • 24,706
  • 6
  • 50
  • 73
-1

You can do this using metaprogramming. The code that adds the property should be run either in the doWithDynamicMethods closure of a plugin or from Bootstrap.groovy. If using the plugin approach, something like this should work:

def doWithDynamicMethods = {ctx ->
  application.domainClasses
        .findAll {it.simpleName.startsWith('S')}.metaClass.each {domainMetaClass ->

    Integer fooVal = 0

    domainMetaClass.getFoo = {-> fooVal}
    domainMetaClass.setFoo = {Integer newFooVal -> fooVal = newFooVal}
  }
}

The code above should add a Integer foo property to every domain class whose name starts with 'S'. I haven't tested this code, so it probably doesn't work. To see an example that you can be more confident about:

  1. Find a plugin that adds that modifies domain classes (e.g. adds a field or method)
  2. Download it
  3. Look at the code in the plugin descriptor's doWithDynamicMethods closure
  4. Copy, paste and adapt to your needs
Dónal
  • 185,044
  • 174
  • 569
  • 824