Reputation: 675
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
Upvotes: 2
Views: 370
Reputation: 2188
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.
Upvotes: 1