Java 在 @Column JPA 注释上设置时,长度属性有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2875149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the length attribute do when set on the @Column JPA annontation?
提问by James McMahon
What exactly does setting the length on a column do in JPA?
在 JPA 中设置列的长度到底有什么作用?
@Column(name = "middle_name", nullable = false, length = 32)
public String getMiddleName() {
return this.middleName;
}
I understand that you can use the annotations to generate the database schema (DDL) based on the entity objects, but does length do any sort of check or truncation when persistence happens, or it solely used for schema creation?
我知道您可以使用注释根据实体对象生成数据库模式 (DDL),但是当持久性发生时,长度是否会进行任何类型的检查或截断,或者它仅用于模式创建?
I also realize that JPA can sit on top of various implementations, the implementation I am concerned with in this case is Hibernate.
我还意识到 JPA 可以位于各种实现之上,在这种情况下我关心的实现是 Hibernate。
采纳答案by Pascal Thivent
Does length do any sort of check or truncation when persistence happens, or it solely used for schema creation?
当持久性发生时,长度是否进行任何类型的检查或截断,或者它仅用于模式创建?
The length
attribute of the Column
annotation is used to specify:
The column length. (Applies only if a string-valued column is used.)
列长度。(仅在使用字符串值列时适用。)
And is only used in the generated DDL. In your example, the resulting column would be generated as a VARCHAR(32)
and trying to insert a longer string would result in an SQL error.
并且仅在生成的 DDL 中使用。在您的示例中,结果列将生成为 aVARCHAR(32)
并且尝试插入更长的字符串将导致 SQL 错误。
For validation, you could add a @Size(max=32)
constraintfrom the Bean Validation API (JSR 303). I provided a sample with a runnable test here.
对于验证,您可以从 Bean 验证 API ( JSR 303)添加@Size(max=32)
约束。我在这里提供了一个带有可运行测试的示例。
Providing both Size
and length
may seem redundant but according to the Appendix D.of the Bean Validation spec, generating Bean Validation-aware DDL is not mandatory for Persistence Providers. So use length
for the DDL, @Size
for validation.
提供了两个Size
和length
看起来是多余的,但根据附录D中的Bean验证规范的,产生Bean验证感知DDL不是强制的持久性提供。所以length
用于 DDL,@Size
用于验证。
In case you're interested, just put a Bean Validation implementation on the classpath with JPA 2.0. With JPA 1.0, refer to this previous answer.
如果您有兴趣,只需在 JPA 2.0 的类路径上放置一个 Bean 验证实现。对于 JPA 1.0,请参阅此先前的答案。
回答by lingaraju p
@column(length=32) is only for DDL purpose and not for restricting means it allows more than 32 characters unless at table level it is not restricted.To restrict size we shold go for @size(max=32)
@column(length=32) 仅用于 DDL 目的而不是用于限制意味着它允许超过 32 个字符,除非在表级别它不受限制。为了限制大小,我们应该使用 @size(max=32)
回答by Ralph
Hibernate 4.3.11 (and other Versions) should pay attention to Validation Annotations. - so you maybe have to upgrade
Hibernate 4.3.11(和其他版本)要注意Validation Annotations。- 所以你可能需要升级
This are cites from Hibernate 4.3.11 manual
这是Hibernate 4.3.11 手册中的引用
Hibernate Core also offers integration with some external modules/projects. This includes Hibernate Validator the reference implementation of Bean Validation (JSR 303) and Hibernate Search.
Hibernate Core 还提供与一些外部模块/项目的集成。这包括 Hibernate Validator Bean Validation (JSR 303) 和 Hibernate Search 的参考实现。
... The integration between Hibernate and Bean Validation works at two levels. First, it is able to check in-memory instances of a class for constraint violations. Second, it can apply the constraints to the Hibernate metamodel and incorporate them into the generated database schema. ...
... Hibernate 和 Bean Validation 之间的集成在两个级别上工作。首先,它能够检查类的内存实例是否违反约束。其次,它可以将约束应用于 Hibernate 元模型并将它们合并到生成的数据库模式中。...
Chapter 22.1.4 Database schema
Hibernate uses Bean Validation constraints to generate an accurate database schema:
@NotNull leads to a not null column (unless it conflicts with components or table inheritance) @Size.max leads to a varchar(max) definition for Strings @Min, @Max lead to column checks (like value <= max) @Digits leads to the definition of precision and scale (ever wondered which is which? It's easy now with @Digits :) )
Hibernate 使用 Bean Validation 约束来生成准确的数据库模式:
@NotNull leads to a not null column (unless it conflicts with components or table inheritance) @Size.max leads to a varchar(max) definition for Strings @Min, @Max lead to column checks (like value <= max) @Digits leads to the definition of precision and scale (ever wondered which is which? It's easy now with @Digits :) )
Note: @Lengh works too, like @Size
注意:@Lengh 也可以工作,比如 @Size
When you use Hibernate Validator 5.1 - then you also need an el-Implementation. For example
当您使用 Hibernate Validator 5.1 时,您还需要一个 el-Implementation。例如
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>el-impl</artifactId>
<version>2.2</version>
</dependency>
If you do not have this, then Hibernate ORM will not been able to start Hibernate Validation, ad therefore it would not take (all) JSR-303 for example @Length
, @Size
in account!
如果你没有这个,则Hibernate ORM将未能启动Hibernate的验证,广告因此不会采取(全部)JSR-303,例如@Length
,@Size
在帐户!