Spring

    Spring consumes와 produces

    Spring consumes와 produces API의 URI를 Mapping할 때 서버에서 수신하는 데이터와 송신하는 데이터 타입을 강제함으로써 오류 상황을 줄일 수 있다. @RequestMapping의 produces 속성을 이용하여 Response의 Content-Type을 제어할 수 있다. @GetMapping, @PostMapping, @PutMapping, @DeleteMapping 등도 지원한다. @Consumes : 수신 하고자하는 데이터 포맷을 정의한다. - 수신 데이터 제한 Cosumes는 클라이언트가 서버에게 보내는 데이터 타입을 명시 @Produces : 응답(출력)하고자 하는 데이터 포맷을 정의한다. - 응답 데이터 제한 Produces는 서버가 클라이언트에게 반환하는 데이터 타입..

    QueryDSL 에러 - Execution failed for task ':compileQuerydsl', ClassNotFoundException: org.gradle.wrapper.GradleWrapperMain

    테스트코드 에러 - Execution failed for task ':compileQuerydsl'. Task :compileQuerydsl FAILED error: cannot find symbol Execution failed for task ':compileQuerydsl'. 같은 에러 발생시에는 Preference -> Build.Execution, Deployment -> Build Tools -> gradle build and run using : Intellij IDEA Run tests using : Intellij IDEA 로 설정하고 다시 테스트하면 테스트가 통과한다. 오류: 기본 클래스 org.gradle.wrapper.GradleWrapperMain을(를) 찾거나 로드할 수 없습니다..

    QueryDSL 설정 - SpringBoot 2.7.X & SpringBoot 3.x 버전

    SpringBoot 2.x대와 SpringBoot 3.x대의 버전 설정 방법이 다르므로 둘에 나눠서 정리 하도록 한다. SpringBoot 2.x버전대 설정 방법 1. Querydsl 버전을 plugins 위에 추가하고 plugins에 querydsl 플러그인을 추가한다. buildscript { ext { queryDslVersion = "5.0.0" } } plugins { id 'java' id 'org.springframework.boot' version '2.7.7' id 'io.spring.dependency-management' version '1.1.0' id "com.ewerk.gradle.plugins.querydsl" version "1.0.10" // querydsl } builds..

    Jpa Insert, update delete 시 select query를 통한 먼저 조회하지 않는 방법.md

    Spring Data Jpa에서는 대부분 JpaRepository interface를 상속받는데, 이 때 기본 구현체로 SimpleJpaRepository.class가 사용된다. 다음은 SimpleJpaRepository의 save method 코드이다. @Transactional @Override public S save(S entity) { Assert.notNull(entity, "Entity must not be null."); if (entityInformation.isNew(entity)) { em.persist(entity); return entity; } else { return em.merge(entity); } } } JpaRepository.save 동작방식은 persist 하거나 m..

    Jpa Id 생성전략에 따른 동작 방식

    JpaRepository.save 동작방식은 persist 하거나 merge를 한다. @Transactional @Override public S save(S entity) { Assert.notNull(entity, "Entity must not be null."); if (entityInformation.isNew(entity)) { em.persist(entity); return entity; } else { return em.merge(entity); } } } 엔티티에 Id 값이 있으면 DB를 조회 (select)한 후에 insert 하게 된다. 생성전략이 존재하지 않을 경우 merge 로 동작. merge 는 입력받은 entity 를 복사한 후 진행하기에 리소스 소모가 있다. 생성전략이 존재하..

    JPA Hibernate Id 생성 전략과 UUID 전략

    JPA 및 Hibernate 내에서 엔티티에 대한 식별자(Id)를 생성하는 다양한 옵션에 대해 정리한다. 식별자는 엔티티 기본 키를 모델링하고 특정 엔터티를 고유하게 식별하는 데 사용된다. 기본 키는 데이터베이스 내의 행을 고유하게 식별한다. 이론적으로 식별자는 기본 키와 일치할 필요가 없다. 식별자는 각 행을 고유하게 식별하는 열에 매핑되기만 하면 되기 때문이다. 여기서는 기본 키와 식별자(Id) 는 일부로 같은 의미로 사용한다. 기본키와(pk) 식별자(Id)는 다음 제약조건이 제한된다 UNIQUE- 값은 각 행을 고유하게 식별해야 한다. NOT NULL- 값은 null일 수 없다. 복합 ID (복합 키)의 경우 어떤 부분도 null이 될 수 없다. IMMUTABLE- 한 번 삽입된 값은 절대 변경할 ..

    Jpa Hibernate Custom Id Generator

    custom ID를 생성하기 위해 Custom Generator 를 생성하기 위해서는 IdentifierGenerator 인터페이스를 이용해서 generate() 메서드를 구현해야 한다. IdentifierGenerator 인터페이스구현 클래스는 반드시 public 으로 선언된 기본 생성자가 있어야한다 package org.hibernate.id; public interface IdentifierGenerator extends Configurable, ExportableProducer { ... Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException; } Identifier..

    Jpa ArrayType. PostgreSQL Array Type을 저장하는 방법 - Hibernate Types, List, Array

    PostgreSQL을 포함한 몇몇 RDB에서는 Array Type의 컬럼 유형을 지원한다. 그러나 hibernate에서 제공해주는 기본 Type들은 Array Type을 지원하지 않는다. 개발자는 Hibernate Type을 Custom 구현해서 사용하거나, 라이브러를 이용해서 PostgreSQL ArrayType을 Spring-data-jpa를 이용해서 hibernate로 매핑할 수 있다. 테스트에 사용된 도메인 엔티티는 다음과 같다. 도메인 엔티티 @Entity public class User { @Id private Long id; private String name; private String[] roles; private List hobbies; } 1. Custom Hibernate Type ..

    Custom Bean Validation (Custom Validation)

    Spring MVC Custom Validation 사용 가능한 constraint 어노테이션이 제공하는 제약 조건 외에 필요한 경우, 직접 만들어서 사용할 수 있다. 임의의 제약(Constraint)과 검증자(Validator)를 구현하여 사용하면 된다. CustomValidation을 위한 Annotation을 생성한다. @Documented @Constraint(validatedBy = ContactNumberValidator.class) @Target( { ElementType.METHOD, ElementType.FIELD }) @Retention(RetentionPolicy.RUNTIME) public @interface ContactNumberConstraint { String message(..

    Java Bean Validation + Validation Tip

    Java Bean Validation + Validation Tip 추천 레퍼런스 페이지 Bean Validation이란? 특정 구현체가 아닌 Bean Validation 2.0(JSR-380)이라는 기술 표준으로 여러 검증 애노테이션과 여러 인터페이스의 모음이다. Spring Boot 3.0.0 M1 릴리스 노트 에서 Spring Boot 2.X는 Jakarta EE를 지원하지 않지만 Spring Boot 3.X에서는 지원이 제공된다. Spring Boot 2.X의 경우 javax. Spring Boot 3.X의 경우 jakarta. 를 사용해야 한다. Bean Validation을 구현한 기술중 일반적으로 사용하는 구현체는 하이버네이트 Validator이다. (이름이 하이버네이트지만 ORM과는 관련없..