我可以在 bean 验证中使用 Hibernate 实体注释吗

问题描述:

要在 JSF 2 中对表单字段使用 bean 验证,您可以在 bean 中使用注释,如 @Length(max=5).

To use bean validation for form fields in JSF 2, you can use annotations in the bean like @Length(max=5).

然而,由于这些约束已经在我的 Hibernate 实体对象中定义,例如:@Column(name = "FIELDX", length = 5),我想知道是否有办法使用他们进行验证?

However, since these constraints are already defined in my Hibernate entity object, for example: @Column(name = "FIELDX", length = 5), I was wondering if there's a way to use them for the validation?

不,你不能.你不应该,因为:

No, you can't. And you shouldn't, because:

  • 在保留字段定义的同时验证逻辑可能会发生变化
  • 只有少数约束可以映射到列定义