Hello all,
I have used Hibernate 3 with annotation for many years. I knew we can override the column attribute by using the @AttributeOverride. Now I am trying to integrate hibernate validator into our system and I get a problem: how to override the validation annotation? I have googled and found nothing, so I post here and hope someone can give me a hand. Thanks.
simple to descript it, I have two domain classes, a base one and a concrete one.
The base one called CodeBean, which contains one one property code. The code looks like (the code about the id is not relevant and ingored here):
Code:
@MappedSuperclass
public abstract class CodeBean {
public static final String PROPERTYNAME_CODE = "code";
private String code;
public CodeBean()
}
// some code for generated id
@NotEmpty
public String getCode() {
return code;
}
public void setCode(String code) {
firePropertyChange(PROPERTYNAME_CODE, this.code, this.code = code);
}
}
The @NotEmpty is used here for validating the code.
The concrete domain class called Country whose code length must be 2. For example "US", "DE". Now I get the Problem, where should I put the annotation @length?
First I have tried override the getCode() method like this:
Code:
@Entity
public class Country extends CodeBean {
@Length(min = 2, max = 2, message = "The code length must be 2")
@Override
public String getCode() {
return super.getCode();
}
// some other code
}
But in this way, I got mapping exception from the hibernate - "Caused by: org.hibernate.MappingException: Duplicate property mapping of code found...... ", when the code was compiled. That is because, with using annotation, we cannot override the property. We can only use the @AttributeOverride for overriding the column attribute of the property.
My question:
1. Have I understood the property override rule of the entity class by using the Hibernate annotation correctly? maybe there is a way to override the property using annotation?
2. Could we override the validation annotation just like the @AttributeOverride does?
thanks for reply.