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

JavaHibernateJpaAnnotations

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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][1]

[Chapter 22.Additional modules][2]

> 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][3]

> ... > 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][4] >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! [1]: http://This%20are%20cites%20from%20Hibernate%204.3.11%20manual [2]: http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#additionalmodules [3]: http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#d5e9984 [4]: http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#d5e10065

Solution 3 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJames McMahonView Question on Stackoverflow
Solution 1 - JavaPascal ThiventView Answer on Stackoverflow
Solution 2 - JavaRalphView Answer on Stackoverflow
Solution 3 - Javalingaraju pView Answer on Stackoverflow