2

I do find a related question with this and I exactly have the same problem. link : Grails FindBy* with an inteface property

Yes, we can't change the Interface property to an abstract property.

I've read that findBy* can't handle interface property but any graceful tips on how we can proceed on this?

As for the models:

interface InterfaceClass {
    //method
}

class EnumClass implements InterfaceClass {
    //implementation of method
}

class NonEnumDomain {
    InterfaceClass interfaceClass
}

Going back to the issue, also regarding the sited link regarding findBy limitations.

NonEnumDomain.findByInterfaceClass(....) won't work.

In case it's needed for the community to know: we did some UserType on these interface properties since it's an Enum properties.

Thanks

Community
  • 1
  • 1
gzz
  • 655
  • 5
  • 18
  • Make another domian class that implements your interface and then use that in your domain. – Sandeep Poonia Aug 25 '15 at 05:06
  • If I understood your comment correctly domain would looked like > interface IterfaceClass { } anotherClass implements InterfaceClass { } domain { AnotherClass anotherClass } `code` is that right? however, on our case, it the below scenario domain { IterfaceClass interfaceClass } – gzz Aug 27 '15 at 03:55
  • sorry for the spam. but for the clarity of it: If I understood your comment correctly domain will have the domain class implementing the interface rather than the domain having the interface as property. is that right? however, on our case, its the later; domain having the interface on its property. – gzz Aug 27 '15 at 04:03
  • Can you post your interface and the domain? – Sandeep Poonia Aug 27 '15 at 08:19
  • I added the set-up on our models for this. the reason for having the NonEnumDomain class has the interfaceClass since there are several EnumClass that is implementing the InterfaceClass. Thanks – gzz Aug 31 '15 at 04:31

1 Answers1

1

Here I'm assuming that you have the full control over the InterfaceClass and for the desired property it has getter and setter methods declared.

So let's say you would like to have two properties named: name and description in your interface class. Create the getter and setter method declaration there and don't declare the properties there.

interface InterfaceClass {
    String getName()

    String getDescription()

    void setName(String name)

    void setDescription(String description)
}

EnumClass class would contain those properties and will implement the InterfaceClass.

class EnumClass implements InterfaceClass {
    String name
    String description

    String getName() {
        return name
    }

    void setiName(String name) {
        this.name = name
    }

    String getDescription() {
        return description
    }

    void setDescription(String description) {
        this.description = description
    }
}

Now to make the finder methods work for your InterfaceClass, you just have to add the interfaceClass property in your domain class to embedded properties list.

class NonEnumDomain {
    InterfaceClass interfaceClass

    static embedded = ['interfaceClass']
}

To save the instance of NonEnumDomain:

new NonEnumDomain(interfaceClass: new EnumClass(name: "Sandeep Poonia", description: "Interface property in domain class")).save(failOnError: true, flush: true)

and to find an instance using finders:

NonEnumDomain.findByInterfaceClass(new EnumClass(name: "Sandeep Poonia", description: "Interface property in domain class")) 

Definition of embedded keyword:

Supports embedding domain components into domain classes. An embedded component does not store its data in its own table as a regular domain class relationship does. Instead, the data is included in the owner's table. The embedded component class is typically declared in the same source file as the owning class or in its own file under src/groovy. Constraints can also be applied over properties of embedded components using Validateable annotation.

Sandeep Poonia
  • 2,158
  • 3
  • 16
  • 29
  • sweet. much appreciated. thanks! PS. sorry I cannot upvote yet due to rep requirement. – gzz Sep 01 '15 at 07:15