cleanUrl: /posts/jpa-column-vs-size-what-is-different
disqus: true
share: true
JPA 관련 글을 읽다가 @Size
라는 annotation 을 발견하게 되었다. @Column
의 length
라는 필드가 있지만, 둘의 차이에 대해 정리한 글이 있어 핵심만 가져왔다. 자세한 내용은 바로 아래 링크를 읽어보면 될것 같다.
Hibernate Tips: Difference Between @Column(length=50) and @Size(max=50)
@Entity
public class Book {
@Id
@GeneratedValue
private Long id;
@Column(length=50)
private String title;
...
}
create table Book (
id int8 not null,
title varchar(50),
version int4 not null,
primary key (id)
)
@Column(length = 50)
을 사용하게 되면 varchar(50) → column에 50개의 char
가 들어가도록 쿼리를 생성한다.
@Entity
public class Book {
@Id
@GeneratedValue
private Long id;
@Size(max=50)
private String title;
...
}
이와 같이 @Size
를 사용할 경우 두가지 이득이 있다.
@Column
의 예시와 같이 DDL statement 가 동일하게 varchar(50) 이 된다.@Size
에 해당하는 값에 맞게 데이터가 할당되었는지 검증한다.둘이 하는짓은 결국 비슷하지만 @Size
의 경우 저장하기 전에 데이터의 검증 절차를 한번 더 갖기 때문에 더 안전하고 강력한 접근을 한다