Skip to content

Commit

Permalink
[FU-339] 상품 중복등록 방지를 위한 제약조건 추가 (#85)
Browse files Browse the repository at this point in the history
* feat: Product 테이블 unique 제약조건 추가

product title, member_id를 결합해 제약조건으로 설정하였음

* feat: DataIntegrityViolationException 예외 처리

제약 조건을 위반(상품 제목 중복 등록 등)했을 경우 예외 처리하기 위함
  • Loading branch information
rheeri authored Nov 10, 2024
1 parent 85c1d7b commit a3cc20a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ public ResponseEntity<Object> handleIllegalArgument(IllegalArgumentException e)
return handleExceptionInternal(errorCode, e.getMessage());
}

@ExceptionHandler(DataIntegrityViolationException.class)
@ExceptionHandler(DataTruncation.class)
public ResponseEntity<Object> handleDataTruncation(DataTruncation e) {
ErrorCode errorCode = CommonErrorCode.INVALID_PARAMETER;
String message = e.getMessage();
return handleExceptionInternal(errorCode, e.getMessage());
}

return handleExceptionInternal(errorCode, message);
@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseEntity<Object> handleDataIntegrityViolation(DataIntegrityViolationException e) {
ErrorCode errorCode = CommonErrorCode.INTERNAL_SERVER_ERROR;
return handleExceptionInternal(errorCode, e.getMessage());
}

// 메서드 인자의 유효성 검사가 실패했을 때 발생
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/foru/freebe/product/entity/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
Expand All @@ -31,6 +33,11 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(
uniqueConstraints = @UniqueConstraint(
name = "unique_title",
columnNames = {"title", "member_id"})
)
public class Product extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down

0 comments on commit a3cc20a

Please sign in to comment.