Partner – JPA Buddy – NPI (tag=JPA/Hibernate)
announcement - icon

The right tools can and will save a lot of time. As long as you are using Hibernate and IntelliJ IDEA you can boost your coding speed and quality with JPA Buddy. It will help in a lot of the day-to-day work:

  • Creating JPA entities that follow best practices for efficient mapping
  • Creating DTOs from entities and MapStruct mappers using convenient visual tools
  • Generating entities from the existing database or Swagger-generated POJOs
  • Visually composing methods for Spring Data JPA repositories
  • Generating differential SQL to update your schema in accordance with your changes in entities
  • Autogenerating Flyway migrations and Liquibase changelogs comparing entities with the database or two databases
  • … and a lot more

Simply put, you'll learn and use the best practices of Hibernate and surrounding technology and become a lot more!

Definitely visit the JPA Buddy site to see its features in action closer.

Persistence top

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

>> CHECK OUT THE COURSE

1. Overview

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.

At first blush, these may seem the same, but they perform different functions.

2. Origins

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.

@Column, though, is a JPA annotation that we use to control DDL statements.

Now let's go through each of them in detail.

3. @Size

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:

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.

4. @Length

As we previously mentioned, @Length is the Hibernate-specific version of @Size. We'll enforce the range for lastName using @Length:

@Entity
public class User {

    // ...
      
    @Length(min = 3, max = 15)
    private String lastName;

    // ...

}

5. @Column(length=value)

@Column is quite different from the previous two annotations.

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:

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

Note that we'll use @Column only to specify table column properties, as it doesn't provide validations.

Of course, we can use @Column together with @Size to specify database column properties with bean validation:

@Entity
public class User {

    // ... 
    
    @Column(length = 5)
    @Size(min = 3, max = 5)
    private String city;

    // ...

}

6. Conclusion

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.

As always, the full source code of the examples is available over on GitHub.

Persistence bottom

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

>> CHECK OUT THE COURSE
Persistence footer banner
Comments are closed on this article!