fix: jpa 로그 출력 설정 추가

수동 ID를 갖는 엔티티 -> Persistable 구현
dev
Lim Jonguk 3 years ago
parent 4d144db227
commit a60c416f95

@ -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<Long> {
//@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;
}
}

@ -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;
}

@ -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<RestErrorResponse> 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);
}

@ -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);
}

@ -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 활성
# ==================================================================================================================

Loading…
Cancel
Save