如何覆盖子类中的@annotations (javax.validation)

How to override @annotations (javax.validation) in a subClass

我有这个Entity,超级棒class:

@Entity
public class Father implements Serializable{    

    @Column(name = "name")
    @Size(min=1, max=10)
    @Pattern(regexp="^[A-Za-z ]*$")
    @NotNull
    private String name;

}

这个,扩展了上面的那个:

@Entity    
public class FatherSub extends Father implements Serializable{  

    private String name;

}

如您所见,在 FatherSub 中我没有 @Annotations 并且我不想要它们。 我怎样才能覆盖它们?有办法吗?

好像注释@NotNull一直存在子里class求我补 Father.name(不是 FatherSub.name),如您在这 2 张 Eclipse 打印屏幕图片中所见。

谢谢

注释仅表示特定时间段的一些元信息,在有子class时不会被覆盖。因此,如何实现注解继承取决于实现。对于 Validator Framework,分散在多个继承 classes 上的注释是 aggregated.

在您的示例中,FatherSub#name 成员 隐藏了 Father#name 成员。这意味着 FatherSub 的所有实例都将与 FatherSub 中定义的 name 一起工作。但是因为 Validation 实现也考虑了继承的注释,虽然 FatherSub#name 没有 @Column 注释,但会弹出错误。您无法更改此行为,因此您必须选择根本不使用继承或为子 class 提供验证器注释而不是父项。

此外,您不需要在 FatherSub 中显式实现 Serializable,因为 Father 已经实现了它。