简体   繁体   English

关注点重复javax.validation.constraints和javax.persistence.Column

[英]Duplication of concerns javax.validation.constraints and javax.persistence.Column

I would like to do some Bean validation of my entities so I can appropriately map the errors to send back to the client. 我想对我的实体进行一些Bean验证,以便可以适当地映射错误以将其发送回客户端。

I understand that the javax.validation.constraints annotations are used to achieve this. 我知道javax.validation.constraints批注用于实现此目的。 My issue is that for my schema generation I need to use the javax.persistence.Column annotation, to describe rules for the persistence layer. 我的问题是,对于我的模式生成,我需要使用javax.persistence.Column批注来描述持久层的规则。 This seems like I am duplicating my logic a little bit. 似乎我在重复一点逻辑。

    @Size( max= Entities.SCHEMA_DESCRIPTION_LENGTH)
    @NotNull
    @Column(name = "Description", length = Entities.SCHEMA_DESCRIPTION_LENGTH)
    private String description;

As you can see I have to specify that the column is 'Not Nullable for both validation purposes and schema generation purposes (same for column length). 如您所见,我必须指定该列“对于验证目的和架构生成目的都是不可为空的(与列长相同)。 This seems redundant. 这似乎是多余的。 Surely there is a way to kill two birds with one stone in this case, as it seems like it would be pretty common, if not standard. 在这种情况下,肯定有一种方法可以用一块石头杀死两只鸟,因为如果不是标准的话,这似乎很常见。

Thanks 谢谢

不幸的是,尽管验证和模式生成有时可能会重叠(例如,列长度和可空性),但是它们之间没有交互,因此您必须分别声明这两个问题(并重复自己)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 @ javax.persistence.Column(updatable = false) - @javax.persistence.Column( updatable=false ) javax.validation.constraints中的转义序列无效 - Invalid escape sequencein javax.validation.constraints 原始布尔值的 javax.validation.constraints? - javax.validation.constraints for primitive boolean? 来自 javax.validation.constraints 的注释不起作用 - Annotations from javax.validation.constraints not working 如何将javax.persistence.Column定义为Unsigned TINYINT? - How can a javax.persistence.Column be defined as an Unsigned TINYINT? 以编程方式“关闭”bean验证(javax.validation.constraints) - “Turn off” bean validation programmatically (javax.validation.constraints) 结合自定义 ConstraintValidator 的 javax.validation.constraints 注释 - Annotations from javax.validation.constraints in combination with custom ConstraintValidator 来自javax.validation.constraints的注释不起作用(忽略) - Annotations from javax.validation.constraints not working (ignored) Java 中的 json 验证器 - 使用 javax.validation.constraints - json validator in Java - using javax.validation.constraints 如何为 `javax.validation.constraints` 注释添加自定义验证器? - how add custom validator for `javax.validation.constraints` annotation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM