announcement - icon

JPA can behave very differently depending on the exact circumstances under which it is used. Code that works in our local environment or in staging performs very poorly (or even flat out fails) when thrown against real-scale databases in production environments.

Debugging these JPA issues in production is pretty difficult - existing APMs don’t provide enough granular insights at the code level, and tracking every single place someone queried entities one by one instead of in bulk can be a grueling, time-consuming task.

Lightrun is a new approach to debugging in production. Using Lightrun’s Logs and Snapshots, you can now get debugger-level granularity in production without opening inbound ports, redeploying, restarting, or even stropping the running application.

In addition, instrumenting Lightrun Metrics at runtime allows you to track down persistence issues securely and in real-time. Want to see it in action? Check out our 2-minute tutorial for debugging JPA performance issues in production using Lightrun:

>> Debugging Spring Persistence and JPA Issues Using Lightrun

1. Overview

In this quick tutorial, we'll take a look at JSR-330‘s @Size, Hibernate‘s @Length and JPA @Column‘s length attribute.

At first blush, these may seem the same, but they perform different functions. Let's see how.

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 to validate the size of a field. The first is a Java-standard annotation and the second 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. Let's 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, this is more portable than @Length.

4. @Length

And as we just stated, @Length is the Hibernate-specific version of @Size. Let's enforce the range for lastName using @Length:

@Entity
public class User {

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

    // ...

}

5. @Column(length=value)

@Column, though, is quite different.

We'll use @Column to indicate specific characteristics of the physical database column. Let's 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;

    // ...

}

Consequently, the resulting column would be generated as a VARCHAR(3) and trying to insert a longer string would 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 property with bean validation.

@Entity
public class User {

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

    // ...

}

6. Conclusion

In this write-up, we learned about the differences between the @Size annotation, @Length annotation and @Column‘s length attribute. 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!