1. Overview
1.概述
In this quick tutorial, we’ll take a look at JSR-330‘s @Size annotation, Hibernate‘s @Length annotation, and JPA‘s @Column‘s length attribute.
在这个快速教程中,我们将看看JSR-330的@Size注释,Hibernate的@Length注释。和JPA的@Column的length属性。
At first blush, these may seem the same, but they perform different functions.
乍一看,这些东西可能看起来是一样的,但它们的功能是不同的。
2. Origins
2.起源
Simply put, all of these annotations are meant to communicate the size of a field.
简单地说,所有这些注释都是为了传达一个字段的大小。
@Size and @Length are similar. We can use either annotation to validate the size of a field. The former is a Java-standard annotation, while the latter is specific to Hibernate.
@Size和@Length类似。我们可以使用这两种注解来验证一个字段的大小。前者是Java标准的注解,而后者是Hibernate特有的。
@Column, though, is a JPA annotation that we use to control DDL statements.
@Column是一个JPA注释,我们用它来控制DDL语句。
Now let’s go through each of them in detail.
现在让我们来详细了解一下它们中的每一个。
3. @Size
3.@大小
For validations, we’ll use @Size, a bean validation annotation. We’ll use the property middleName annotated with @Size to validate its value between the attributes min and max:
对于验证,我们将使用@Size,一个Bean验证注解。我们将使用带有@Size注解的属性middleName来验证其在属性min和max:之间的值。
public class User {
// ...
@Size(min = 3, max = 15)
private String middleName;
// ...
}
Most importantly, @Size makes the bean independent of JPA and its vendors, such as Hibernate. As a result, it’s more portable than @Length.
最重要的是,@Size使bean独立于JPA及其供应商,例如Hibernate。因此,它比@Length更容易移植。
4. @Length
4.@长度
As we previously mentioned, @Length is the Hibernate-specific version of @Size. We’ll enforce the range for lastName using @Length:
正如我们之前提到的,@Length是Hibernate特定版本的@Size。我们将使用@Length来强制执行lastName的范围。
@Entity
public class User {
// ...
@Length(min = 3, max = 15)
private String lastName;
// ...
}
5. @Column(length=value)
5.@Column(length=value)
@Column is quite different from the previous two annotations.
@Column与前两个注释完全不同。
We’ll use @Column to indicate specific characteristics of the physical database column. We’ll use the length attribute of the @Column annotation to specify the string-valued column length:
我们将使用@Column来表示物理数据库列的具体特征。我们将使用length属性的@Column注释来指定字符串值的列长度。
@Entity
public class User {
@Column(length = 3)
private String firstName;
// ...
}
The resulting column will be generated as a VARCHAR(3), and trying to insert a longer string will result in an SQL error.
产生的列将被生成为VARCHAR(3),,试图插入一个更长的字符串将导致SQL错误。
Note that we’ll use @Column only to specify table column properties, as it doesn’t provide validations.
注意,我们将使用@Column来指定表的列属性,因为它不提供验证。
Of course, we can use @Column together with @Size to specify database column properties with bean validation:
当然,我们可以使用@Column和@Size来指定带有bean验证的数据库列属性。
@Entity
public class User {
// ...
@Column(length = 5)
@Size(min = 3, max = 5)
private String city;
// ...
}
6. Conclusion
6.结语
In this article, we learned about the differences between the @Size annotation, @Length annotation, and @Column‘s length attribute. Then we examined each separately within the areas of their use.
在这篇文章中,我们了解了@Size注释、@Length注释和@Column的length属性之间的区别。然后我们在它们的使用范围内分别进行了检查。
As always, the full source code of the examples is available over on GitHub.
一如既往,这些示例的完整源代码可在GitHub上获得over。