简体   繁体   中英

What does the length attribute do when set on the @Column JPA annontation?

What exactly does setting the length on a column do in 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?

I also realize that JPA can sit on top of various implementations, the implementation I am concerned with in this case is Hibernate.

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.


For validation, you could add a @Size(max=32) constraint from the Bean Validation API ( JSR 303 ). I provided a sample with a runnable test here .

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.

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 .

Hibernate 4.3.11 (and other Versions) should pay attention to Validation Annotations. - so you maybe have to upgrade

This are cites from Hibernate 4.3.11 manual

Chapter 22.Additional modules

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.

Chapter 22.1 Bean Validation

... 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. ...

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 :) )

Note: @Lengh works too, like @Size


When you use Hibernate Validator 5.1 - then you also need an el-Implementation. For example

<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!

@Column(length=32)仅用于 DDL 目的而不是用于限制意味着它允许超过 32 个字符,除非在表级别它不受限制。要限制大小,我们应该使用@Size(max=32)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM