Sean LeBlanc
Sean LeBlanc

Reputation: 195

Grails 3 @Delegate notation, using a domain object

Under Grails 2.4.4, we had classes we used as wrappers for domain objects.

They would look like this:

class Foo {
  @Delegate
  OurDomainClass ourDomainClass
  ...

}

This worked, but when trying to compile under Grails 3.0.11, we get this:

> Foo.groovy: 14: Can't have an abstract method in a non-abstract class.
> The class 'Foo' must be declared abstract or the method
> 'org.springframework.validation.Errors
> org_grails_datastore_gorm_GormValidateable__errors$get()' must be
> implemented.  @ line 14, column 1.    class Foo {    ^

Removing the @Delegate annotation will make compilation pass, but calls to methods of the underlying class obviously then do not work.

Is there a way to work around this or to achieve this same behavior and have it pass compilation under Grails 3?

Upvotes: 8

Views: 605

Answers (3)

Maicon Mauricio
Maicon Mauricio

Reputation: 3011

@timbonicus' solution works for Grails 3, but on Grails 4 it fails with multiple errors.

Ended up changing the @Delegate classes to Groovy traits as an alternative. It will still be separated, as traits support multiple inheritance and implemented methods. Also, it won't be necessary to add another call to an object as embedded, hasMany and hasOne would require.

trait OurDomainClass {}

trait OurOtherDomainClass {}

class Foo implements OurDomainClass, OurOtherDomainClass {}

If you are creating a plugin, you can add methods at compile time with traits.

Upvotes: 0

timbonicus
timbonicus

Reputation: 908

You can work around this by changing the wrapper class to implement the GORM traits:

class Foo implements GormValidateable, DirtyCheckable, Validateable {
    @Delegate
    OurDomainClass ourDomainClass
    ...
}

I went further and created my own interface:

class Foo implements GormDelegateHack {
    @Delegate
    OurDomainClass ourDomainClass
    ...
}

interface GormDelegateHack extends GormValidateable, DirtyCheckable, Validateable {
}

I filed issue #856 against grails-data-mapping, although it may be a Groovy bug.

Upvotes: 0

Kamil Węglarz
Kamil Węglarz

Reputation: 56

Does good old static hasMany = [] or static hasOne = [] won't do the job? Of course wrappers would be domain classes then too.

Upvotes: 1

Related Questions