@NotNull 검증 어노테이션

2022. 8. 25. 21:24·개발/spring boot
728x90

1.검증

@Column(nullable = false)

  • DB에 Insert 쿼리를 저장 하는 상황에서 예외를 터트리게 됩니다.

@NotNull

  • 검증 어노테이션 이것을 더 선호합니다.
  • 어노테이션을 쓰면, 데이터베이스에 SQL 쿼리를 보내기 전에 예외가 발생한다.
  • Repository에 잘못된 엔티티를 저장 때, ConstraintViolationException 를 터뜨리므로 더 빠른 순간에 문제를 잡아낼 수 있습니다.
  1. @NotNull 사용 방법
dependencies {

    implementation 'org.springframework.boot:spring-boot-starter-validation'
 
}
  • application.properties
spring.jpa.properties.hibernate.check_nullability=true
  • Member
@Entity
@Getter @Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@ToString(of = {"id","member_userid"})
public class Member {
	@Id
	@GeneratedValue
	@Column(name="member_id")
	private Long id;
	
	private String member_userid;
	
	private String member_passwd;
	@NotNull
	private String member_username;
	
	private String member_phone;
	
	@OneToMany(mappedBy = "member")
	List<Board> boards = new ArrayList<>();
	
	public Member(String member_userid,String member_username,String member_passwd, String member_phone) {
		this.member_userid = member_userid;
		this.member_username = member_username;
		this.member_passwd = member_passwd;
		this.member_phone = member_phone;
	}
	
}

참고

  • URL : https://jeong-pro.tistory.com/233
728x90

'개발 > spring boot' 카테고리의 다른 글

[ToStringBuilder] toString()을 json형식으로 확인  (0) 2022.09.27
[어노테이션] @MappedSuperclass  (0) 2022.09.07
spring boot STS profile 설정 (The Class-Path manifest attribute in [path] referenced 이슈)  (0) 2022.08.03
spring 엑셀 다운로드 예제 정리  (0) 2022.07.26
spring boot & IntelliJ IDEA & gradle 빌드 하는 방법  (0) 2021.01.24
'개발/spring boot' 카테고리의 다른 글
  • [ToStringBuilder] toString()을 json형식으로 확인
  • [어노테이션] @MappedSuperclass
  • spring boot STS profile 설정 (The Class-Path manifest attribute in [path] referenced 이슈)
  • spring 엑셀 다운로드 예제 정리
nix-be
nix-be
  • nix-be
    NiX
    nix-be
  • 전체
    오늘
    어제
    • 홈
      • 책
        • 오브젝트
      • 성장
        • jpa Querydsl 정리
        • 코딩테스트
      • 인프라
        • linux
        • vmware
        • CI&CD
        • 네트워크
        • docker
      • 개발
        • spring boot
        • JPA
        • java
        • thymeleaf
        • 이슈
        • jquery
        • javascript
        • 안드로이드
      • DB
        • postgreSql
      • 잡다한것
        • 프로그램
        • 일상 관련
      • 회사
        • 티
  • 블로그 메뉴

    • 홈
    • 개발
  • 링크

  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
nix-be
@NotNull 검증 어노테이션
상단으로

티스토리툴바