67
votes

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.

3
Btw, if you don't set the length, it will be 255 by default (tested with MySQL)cn123h

3 Answers

93
votes

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.

6
votes

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!

4
votes

@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 should go for @Size(max=32)