문제

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?

도움이 되었습니까?

해결책

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()
 }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top