Question

Is it a good idea to override setters and getters of properties in domain class? Assume we have a domain class with name property and capitalizedName where we store clean up name:

class Person {
    String name
    String capitalizedName

    String setName(String name){
        this.name = name
        this.searchName = name.replaceAll("[^A-Za-z0-9 ]", "").trim().toUpperCase()
    }
}

If I override setter and in a unit test try to use dynamic finder:

Person.findByName('Whatever')

I got

java.lang.IllegalArgumentException: Property [name] is not a valid property of class [com.test.Person]

But in runtime it works pretty fine.

Can I modify getters and setters of a domain class? What is the best way to achieve behaviour as I described above?

Was it helpful?

Solution

A setter should have a return type of void

 void setName(String name){
        this.name = name
        this.searchName = name.trim().replaceAll("[^A-Za-z0-9 ]", "").replaceAll(" +", " ").toUpperCase()
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top