1
votes

I have the following class structure

public Abstract class Person {
    private String fullName;
    private Address address;
    private Phone ;
}

class Staff extends Person{
    private String staffId;
}

I want to apply validation using JSR-303 on class Staff whereby Staff address and phone cannot have the value of null. However, I have some other classes that are class of Person where I don't wish to have the validation to be applied.

One way to do this that I could think of is by refactor Person and push the fields 'address' and 'phone' to Staff, but this means refactoring a lot of other classes (and not to mention redundancy this shall cause), and hence something I want to avoid.

Update. I have changed Staff class, as follows

public class Staff extends Person {
    @NotNull
    private String staffEmploymentId;

    public String getStaffEmploymentId() {
        return staffEmploymentId;
    }

    public void setStaffEmploymentId(String id) {
        this.staffEmploymentId = id;
    }

    @Override
    @NotNull
    public void setPhones(List<Phone> phones) {
        super.phones = phones;
    }

    @Override
    @NotNull
    public void setAddress(Address a) {
        super.address = a;
    }

    @Override
    @NotNull
    public Address getAddress(){
        return super.address;
    }
}

However, I've got the following error.

javax.validation.ValidationException: Property setAddress does not follow javabean conventions.

I am using Apache BVal, as opposed to Hibernate Validator.

1

1 Answers

1
votes

Annotate getters instead of fields using annotations from JSR-330. You can override getters in Stuff and annotate them.