@Size와 @Length는 필드 크기의 유효성을 검사하는 데 사용된다는 점이 유사하다.
Bean Validation API에서 제공하는 annotation으로, 주로 입력 값의 길이나 크기를 제한할 때 사용한다.
이 annotation을 사용하면 문자열의 길이나 컬렉션의 크기 등을 유효성 검사할 수 있습니다.
예를 들어, @Size(min=2, max=15)와 같이 사용하여 문자열의 길이가 2에서 15 사이여야 한다는 제약 조건을 설정할 수 있다.
@Entity
public class User {
@Id
@GeneratedValue
private Long userId;
@Size(min = 2, max = 15)
private String nickname;
...
}
Hibernate Validator에서 제공하는 @Size annotation이다. 문자열의 최대 또는 최소 길이를 지정할 수 있다. 예를 들어, @Length(min = 2, max=15)과 같이 사용하여 문자열의 최소 길이 2, 최대 길이를 15로 제한할 수 있다.
@Entity
public class User {
...
@Length(min = 2, max = 15)
private String nickname;
...
JPA(Java Persistence API)에서 제공하는 annotation으로, length 속성을 사용하여 데이터베이스 테이블의 열(Column)의 길이를 지정할 수 있다.
이 annotation은 데이터베이스 스키마에 직접 영향을 주는 것이기 때문에 데이터베이스에 데이터를 저장할 때 해당 길이 제약이 적용된다.
@Entity
public class User {
...
@Column(length = 15)
private String nickname;
...
참고
21.1 Using Bean Validation Constraints
Annotation Type Column
Annotation Type Length
Difference Between @Size, @Length, and @Column(length=value)
Hibernate Tips: What’s the Difference Between @Column(length=50) and @Size(max=50)