diff --git a/src/main/java/com/xit/biz/ctgy/entity/MinInfoBoard680.java b/src/main/java/com/xit/biz/ctgy/entity/MinInfoBoard680.java index a8f6cba..dc72bf0 100644 --- a/src/main/java/com/xit/biz/ctgy/entity/MinInfoBoard680.java +++ b/src/main/java/com/xit/biz/ctgy/entity/MinInfoBoard680.java @@ -6,12 +6,17 @@ import io.swagger.v3.oas.annotations.media.Schema; import lombok.*; import org.hibernate.annotations.DynamicInsert; import org.hibernate.annotations.DynamicUpdate; +import org.springframework.data.domain.Persistable; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; import javax.persistence.*; -import java.io.Serializable; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +/** + * ID를 수동으로 set 하는 경우 + * Persistable 구현 필요 : persitance 처리시 신규인지 체크에 필요한 getId 와 isNew Override + */ @Schema(name = "MinInfoBoard680", description = "공지사항") @Table(name = "min_info_board680") @Entity @@ -21,8 +26,7 @@ import java.time.format.DateTimeFormatter; @Builder @DynamicInsert @DynamicUpdate -public class MinInfoBoard680 implements Serializable { - private static final long SerialVersionUID = 1L; +public class MinInfoBoard680 implements Persistable { //@Query(value = "SELECT max(mib.inCode) + 1 FROM MinInfoBoard680 mib WHERE mib.inBgubun = '1'") @Id @@ -89,6 +93,8 @@ public class MinInfoBoard680 implements Serializable { @Setter private String inEtc; + + @PrePersist public void onPrePersist(){ LocalDateTime localDateTime = LocalDateTime.now(); @@ -107,4 +113,14 @@ public class MinInfoBoard680 implements Serializable { // this.inName = HeaderUtil.getUserName(); // this.inHit = this.inHit + 1; } + + @Override + public Long getId() { + return this.inCode; + } + + @Override + public boolean isNew() { + return this.inCode == 0L && this.inNalja == null; + } } diff --git a/src/main/java/com/xit/biz/ctgy/service/impl/CtgyFileService.java b/src/main/java/com/xit/biz/ctgy/service/impl/CtgyFileService.java index a00cd01..8f9edcb 100644 --- a/src/main/java/com/xit/biz/ctgy/service/impl/CtgyFileService.java +++ b/src/main/java/com/xit/biz/ctgy/service/impl/CtgyFileService.java @@ -7,7 +7,6 @@ import com.xit.biz.ctgy.repository.IPublicBoardRepository; import com.xit.biz.ctgy.service.ICtgyFileService; import com.xit.core.constant.ErrorCode; import com.xit.core.exception.CustomBaseException; -import com.xit.core.support.jpa.JpaUtil; import com.xit.core.util.AssertUtils; import com.xit.core.util.Checks; import com.xit.core.util.DateUtil; @@ -104,7 +103,7 @@ public class CtgyFileService implements ICtgyFileService { savedEntity.setInFileurl(serviceUrl + urlPath); setEntity(savedEntity, dto); } - JpaUtil.saveIfNullId(dto.getInCode(), repository, savedEntity); + repository.save(savedEntity); entityList.add(savedEntity); mf.transferTo(new File(fileUploadPath + File.separator + orgFileName)); @@ -132,7 +131,8 @@ public class CtgyFileService implements ICtgyFileService { savedEntity = repository.findById(dto.getInCode()).orElseThrow(() -> new CustomBaseException(ErrorCode.NOT_FOUND)); setEntity(savedEntity, dto); } - JpaUtil.saveIfNullId(dto.getInCode(), repository, savedEntity); + repository.save(savedEntity); + //JpaUtil.saveIfNullId(dto.getInCode(), repository, savedEntity); } return entityList; } diff --git a/src/main/java/com/xit/core/exception/handling/CustomRestExceptionHandler.java b/src/main/java/com/xit/core/exception/handling/CustomRestExceptionHandler.java index c6d84d0..d13bea6 100644 --- a/src/main/java/com/xit/core/exception/handling/CustomRestExceptionHandler.java +++ b/src/main/java/com/xit/core/exception/handling/CustomRestExceptionHandler.java @@ -6,11 +6,14 @@ import com.xit.core.exception.CustomBaseException; import com.xit.core.util.Checks; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; +import org.hibernate.JDBCException; +import org.hibernate.exception.GenericJDBCException; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.orm.jpa.JpaSystemException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; @@ -18,6 +21,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; +import javax.persistence.PersistenceException; import javax.validation.ConstraintViolationException; import java.util.HashMap; import java.util.Map; @@ -189,6 +193,12 @@ public class CustomRestExceptionHandler extends ResponseEntityExceptionHandler { protected ResponseEntity handleRuntimeException(RuntimeException e) { String message = Checks.isNotNull(e) ? e.getLocalizedMessage() : StringUtils.EMPTY; log.error("handleException RuntimeException : {}", Checks.isEmpty(message) ? StringUtils.EMPTY : e.getClass().getCanonicalName()); + + // Hibernate SQL 예외인 경우 메세지 획득 + if(e instanceof PersistenceException || e instanceof JDBCException || e instanceof JpaSystemException || e instanceof GenericJDBCException){ + message = ((GenericJDBCException) e.getCause()).getSQLException().getLocalizedMessage(); + //message = ((GenericJDBCException) e.getCause()).getSQLException().getLocalizedMessage(); + } return RestErrorResponse.of(HttpStatus.INTERNAL_SERVER_ERROR.toString(), message); } diff --git a/src/main/java/com/xit/core/support/jpa/JpaUtil.java b/src/main/java/com/xit/core/support/jpa/JpaUtil.java index 2b70a88..ba6ac0f 100644 --- a/src/main/java/com/xit/core/support/jpa/JpaUtil.java +++ b/src/main/java/com/xit/core/support/jpa/JpaUtil.java @@ -32,7 +32,7 @@ public class JpaUtil { * @param repository JpaRepository * @param entity Object */ - @SuppressWarnings("rawtypes") + @SuppressWarnings({"unchecked", "rawtypes"}) public static void saveIfNullId(Long id, JpaRepository repository, Object entity) { if(id == null) repository.save(entity); } @@ -43,7 +43,7 @@ public class JpaUtil { * @param repository JpaRepository * @param entity Object */ - @SuppressWarnings("rawtypes") + @SuppressWarnings({"unchecked", "rawtypes"}) public static void saveIfNullId(String id, JpaRepository repository, Object entity) { if(id == null) repository.save(entity); } diff --git a/src/main/resources/config/application-dev.yml b/src/main/resources/config/application-dev.yml index 2ee9cbc..c027bdf 100644 --- a/src/main/resources/config/application-dev.yml +++ b/src/main/resources/config/application-dev.yml @@ -49,13 +49,25 @@ decorator: enable-logging: true logging: level: + root: INFO # spring.mvc.log-request-details=debug 활성화 web: debug - root: info - # hibernate sql log 출력시 변수 바인딩 org: + springframework: + transaction: + interceptor: TRACE + orm: + jpa: TRACE + # hibernate sql log 출력시 변수 바인딩 hibernate: - #type: trace + SQL: + debug + type: + descriptor: + sql: + BasicBinder: debug + + # ================================================================================================================== # Spring-doc 활성 # ==================================================================================================================