使用JPA和Hibernate时@Size,@ Longth和@Column(length = value)之间的区别 [英] Difference between @Size, @Length and @Column(length=value) when using JPA and Hibernate

查看:514
本文介绍了使用JPA和Hibernate时@Size,@ Longth和@Column(length = value)之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下三个字段的验证检查之间有什么区别?

What is the difference between the validation check of the following three fields?

@Entity
public class MyEntity {

    @Column(name = "MY_FIELD_1", length=13)
    private String myField1;
    @Column(name = "MY_FIELD_2")
    @Size(min = 13, max = 13)
    private String myField2;
    @Column(name = "MY_FIELD_3")
    @Length(min = 13, max = 13)
    private String myField3;

    // getter & setter

}

我读到第一个与DDL有关东西。
第二个用于bean验证。
第三个用于休眠验证。

I read that the first one has to do with DDL stuff. The second is for bean-validation. The third is for hibernate-validation.

这是正确的吗?我还不明白的是:我什么时候需要使用哪一个?这些注释中的一个何时触发?

Is that correct? What I still don't understand is: When do I have to use which one? When does one of these annotations trigger?

编辑:考虑以下情况:
鉴于需要开发具有字符串类型字段的实体13.您会选择上述哪种方法?甚至更好:你有哪些问题要问自己要找出哪一个适合你的目的?

Think of the following situation: Given the requirement to develope an entity with a field of type string with length 13. Which of above mentioned methods would you choose? Or even better: Which questions do you have to ask yourself to find out which one suits your purposes?

推荐答案


  1. @Column 是JPA注释,架构生成工具使用 length 属性来设置关联的SQL列长度。

  2. @Size 是一个Bean Validation批注,用于验证关联的String是否具有长度受最小值和最大值限制的值。

  3. @Length 是一个特定于Hibernate的注释,其含义与 @Siz相同e

  1. @Column is a JPA annotation and the length attribute is used by the schema generation tool to set the associated SQL column length.
  2. @Size is a Bean Validation annotation that validates that the associated String has a value whose length is bounded by the minimum and maximum values.
  3. @Length is a Hibernate-specific annotation and has the same meaning as @Size

所以 2。 3. 应使用Bean Validation验证 String 长度。我选择 2。因为它是通用的。

So both 2. and 3. should validate the String length using Bean Validation. I'd pick 2. because it's generic.

这篇关于使用JPA和Hibernate时@Size,@ Longth和@Column(length = value)之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆